Reputation: 21877
The AlarmManager sends the intent after a minute in the activities onDestroy method.
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
new Intent(this, MyService.class),
PendingIntent.FLAG_NO_CREATE);
AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.MINUTE, 1);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
The intent send by the alarmManager is expected to invoke the MyService::onStartCommand method which will log the message "OnReceive". But this does not happen.
public class MyService extends Service {
private static final String TAG = "MyService";
static public MediaPlayer mp;
public MyService() {
}
@Override
public int onStartCommand (Intent intent, int flags, int startId) {
Log.d(TAG, "onReceive");
}
}
Upvotes: 0
Views: 66
Reputation: 1371
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
new Intent(this, MyService.class),
PendingIntent.FLAG_NO_CREATE);
with this,
PendingIntent pendingIntent = PendingIntent.getService(this, 0,
new Intent(this, MyService.class),
PendingIntent.FLAG_NO_CREATE);
If you have any query, Please let me know.
Upvotes: 1