运行Broadcastreceiver指定的时间

时间:2013-12-10 07:58:30

标签: android android-service android-broadcast

我是android的新手,我想只在特定时间内运行任务,我该怎么做? 我尝试使用广播接收器和AlarmManager,但广播接收器甚至在指定时间后仍然连续运行。

long timetorun= calendarcurrent.getTimeInMillis() - calendar.getTimeInMillis();
Intent c = new Intent(getApplicationContext(),
Phonecallreceiver.class); 
pintent = PendingIntent.getBroadcast(getApplicationContext(), 0, c, 0);
AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, timetorun, pintent);

1 个答案:

答案 0 :(得分:0)

你可以通过取消警报来停止它,使用相同的PendingIntent,如下所示:

Intent c = new Intent(getApplicationContext(), Phonecallreceiver.class); 
pintent = PendingIntent.getBroadcast(getApplicationContext(), 0, c, 0);
AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE); 
alarm .cancel(pintent);

不要忘记确保PendingIntent不为NULL。 希望我帮助过你。