sunflower20
sunflower20

Reputation: 499

BroadcastReceiver is not called from AlarmManager

I wan't to see how AlarmManager setExactAndAllowWhileIdle for Oreo works and wrote very simple service for that, but it doesn't work and I really can't get the reason. All the reasons I've seen so far did not concern to my code. Please guide where I went wrong.

MainActivity

   private void startService() {
            Log.d("testt", "MainActivity - startService");
            Intent intent = new Intent(this, LocationReceiver.class);
           PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1,intent,PendingIntent.FLAG_UPDATE_CURRENT ); 
 AlarmManager alarmManager =(AlarmManager)getSystemService(ALARM_SERVICE);
                    alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, System.currentTimeMillis() +  60 * 1000, pendingIntent);
         }

BroadcastReceiver

public class LocationReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("testt", "LocationReceiver - onReceive");
        Intent i = new Intent(context, LocationService.class);
        context.startService(intent);
    }
}

Service

public class LocationService extends IntentService {
    public LocationService() {
        super("any_name");
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        Log.d("testt", "LocationService - onHandleIntent");
    }
}

Finally Manifest

    <service android:name=".LocationService" android:exported="false"/>
    <receiver android:name=".LocationReceiver"/>
</application>

Upvotes: 3

Views: 1581

Answers (3)

Ajay Valand
Ajay Valand

Reputation: 13

some time alarm receiver not call exact time wait some time if it not call.

for service change your broadcast receiver code

public class LocationReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    Log.d("testt", "LocationReceiver - onReceive");
    Intent i = new Intent(context, LocationService.class);
    context.startForegroundService(intent);
}}

In your service OnCreate Method. startForeground(11,buildForegroundNotification());

Upvotes: 0

Scott Kronheim
Scott Kronheim

Reputation: 772

Regarding this line of code in your activity:

alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, System.currentTimeMillis() +  60 * 1000, pendingIntent);

Instead of System.currentTimeMillis(), you want to use SystemClock.elapsedRealtime(); your code should look like this:

alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() +  60 * 1000, pendingIntent);

You need to start your interval time calculation with the number of milliseconds since device bootup, not the number of milliseconds since 1/1/1970.

Upvotes: 2

Pritesh Patel
Pritesh Patel

Reputation: 698

You can try given code. You can directly call LocationService like this or refer this code Autologout

Intent myIntent = new Intent(this, LocationService.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.MINUTE, 2);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

Upvotes: 0

Related Questions