我像这样在MainActivity中创建AlarmManager
AlarmManager mgr = (AlarmManager) getApplicationContext()
.getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent(getApplicationContext(),
TransactionService.class);
PendingIntent pendingIntent=PendingIntent.getService(getApplicationContext(), 0, notificationIntent, 0);
mgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(), 2000, pendingIntent);
我想在某种情况下停止广播接收器中的此警报管理器。在广播接收器的代码下方(无论它是什么)。
public class IncomingSms extends BroadcastReceiver {
private Intent notificationIntent;
public void onReceive(Context context, Intent intent) {
}
}
}
那么,如何访问此警报管理器并将其暂停到广播接收器中,又如何重新启动它呢?
public class IncomingSms extends BroadcastReceiver {
private Intent notificationIntent;
public void onReceive(Context context, Intent intent) {
AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent(context, TransactionService.class);
PendingIntent pendingIntent=PendingIntent.getService(getApplicationContext(), 0, notificationIntent, 0);
mgr.cancel(pendingIntent)
}
}
您只需要重建PendingIntent,然后将其传递给AlarmManager的cancel()方法即可。PendingIntent的标识将通过检查PendingIntent的ID以及包含的Intent是否满足在此处定义的filterEquals()-requirements来完成:http : //developer.android.com/reference/android/content/Intent.html #filterEquals%28android.content.Intent%29
详细信息:http : //developer.android.com/reference/android/app/AlarmManager.html#cancel%28android.app.PendingIntent%29
如果要重新启动,只需再次注册到PendingIntent。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句