Need code example on how to run an Android service forever in the background even when device sleeping, like Whatsapp?

NOTE: NOW THIS ANSWER IS ONLY VALID FOR ANDROID 7 AND BELOW. SINCE ANDROID 8 GOOGLE HAS CHANGED HOW BACKGROUND TASKS ARE HANDLED Since I posted this question, I have implemented two different approaches to this solution into multiple apps. APPROACH 1 This extract is from an app where I use push notifications, which need … Read more

How to show daily offline notifications in Android 10?

I can see one possible issue, and it’s possible to do this without the workmanager (assuming the device has a healthy connection at the time the notification runs). Instead of doing your network in the receiver itself, I suggest starting a service (foreground service if above Android 8.0), and doing your work there. This is … Read more

i want show notification at 8:00 am everyday

alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmIntent = new Intent(context of current file, AlarmReceiver1.class); pendingIntent = PendingIntent.getBroadcast(Menu.this, 0, alarmIntent,PendingIntent.FLAG_UPDATE_CURRENT); alarmIntent.setData((Uri.parse(“custom://”+System.currentTimeMillis()))); alarmManager.cancel(pendingIntent); Calendar alarmStartTime = Calendar.getInstance(); Calendar now = Calendar.getInstance(); alarmStartTime.set(Calendar.HOUR_OF_DAY, 8); alarmStartTime.set(Calendar.MINUTE, 00); alarmStartTime.set(Calendar.SECOND, 0); if (now.after(alarmStartTime)) { Log.d(“Hey”,”Added a day”); alarmStartTime.add(Calendar.DATE, 1); } alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmStartTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent); Log.d(“Alarm”,”Alarms set for everyday 8 am.”); Coming to the … Read more

Is it possible to create multiple PendingIntents with the same requestCode and different extras?

Actually, you don’t “create” PendingIntents. You request them from the Android framework. When you request a PendingIntent from the Android framework, it checks to see if there is already a PendingIntent that matches the criteria you pass as arguments. If so, it does not create a new PendingIntent, it just gives you back a “token” … Read more

android: running a background task using AlarmManager

Here is one complete example: http://android-in-practice.googlecode.com/svn/trunk/ch02/DealDroidWithService/ The pattern this example uses, and one that I’ve found that seems to work well, is to use a boot receiver to setup the AlarmManager (and of course also check to start the polling from your main Activity too, for the case when your app is installed and the … Read more

How can I correctly pass unique extras to a pending intent?

Possibly two different issues here: 1) If you’ve already created your PendingIntent before and it “matches” an existing PendingIntent, then you must specify the PendingIntent.FLAG_UPDATE_CURRENT flag or it will not pass the extras. A “match” is based on the criteria that Intent.filterEquals() uses, so definitely read the docs there and make sure you understand the … Read more

Android: Get all PendingIntents set with AlarmManager

You don’t have to keep reference to it. Just define a new PendingIntent like exactly the one that you defined in creating it. For example: if I created a PendingIntent to be fired off by the AlarmManager like this: Intent alarmIntent = new Intent(getApplicationContext(), AlarmBroadcastReceiver.class); alarmIntent.setData(Uri.parse(“custom://” + alarm.ID)); alarmIntent.setAction(String.valueOf(alarm.ID)); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); PendingIntent … Read more

Can’t stop the ringing alarm from another activity

Your architecture is broken. You don’t use a BroadcastReceiver for persistent processing. A BroadcastReceiver has a very short lifecycle, you use it to trigger other things. You’ve created a MediaPlayer instance in your BroadcastReceiver and are trying to control that in onReceive(). This is wrong. You should use a Service to manage and maintain the … Read more

setExactAndAllowWhileIdle – is not exact as of developer reference

So how can I achieve an exact alarm with AlarmManager in 6.0? You are welcome to try setAlarmClock(), as AFAIK it is unaffected by Doze mode. Otherwise, AlarmManager is not a viable option for you. Even having your app on the battery optimization whitelist will not help, as AlarmManager behavior does not change based on … Read more