服务未收到AlarmManager PendingIntent

时间:2015-01-15 06:56:46

标签: android

AlarmManager在活动 onDestroy 方法中一分钟后发送意图。

    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);

alarmManager发送的意图应该调用MyService :: onStartCommand方法  这将记录消息" OnReceive"。但这不会发生。

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");
    }
}

1 个答案:

答案 0 :(得分:1)

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
            new Intent(this, MyService.class),
            PendingIntent.FLAG_NO_CREATE);

有了这个,

PendingIntent pendingIntent = PendingIntent.getService(this, 0,
            new Intent(this, MyService.class),
            PendingIntent.FLAG_NO_CREATE);

如果您有任何疑问,请告诉我。