问题描述
如何PendingIntent.cancel()影响AlarmManager如果有一个未决的报警。
How does PendingIntent.cancel() affect AlarmManager if there is a pending alarm.
我应该呼吁取消对两个对象(意向和alarmmanager)取消报警?有人能解释它们如何协同工作。
Should I call cancel on both objects(intent and alarmmanager) to cancel an alarm? Can someone explain how they work together.
先谢谢了。
推荐答案
注册PendingIntents
实例的PendingIntent
PendingIntent instance can be obtained via factory methods PendingIntent.getActivity(), PendingIntent.getService(), PendingIntent.getBroadcast().
不过,除了刚刚获得的PendingIntent例如,ActivityManager注册的PendingIntent在内部缓存/元数据文件,如果它不存在。在相反,如果它确实存在,则返回previously注册实例。
However, apart from just obtaining PendingIntent instance, ActivityManager registers the PendingIntent in internal cache/meta data file if it does not exist. In contrary, if it does exist, then previously registered instance is returned.
例如,
public static PendingIntent getActivity(Context context, int requestCode,
Intent intent, int flags) {
String packageName = context.getPackageName();
String resolvedType = intent != null ? intent.resolveTypeIfNeeded(
context.getContentResolver()) : null;
try {
intent.setAllowFds(false);
IIntentSender target =
ActivityManagerNative.getDefault().getIntentSender(
IActivityManager.INTENT_SENDER_ACTIVITY, packageName,
null, null, requestCode, new Intent[] { intent },
resolvedType != null ? new String[] { resolvedType } : null, flags);
return target != null ? new PendingIntent(target) : null;
} catch (RemoteException e) {
}
return null;
}
随着文档状态:
/**
* Retrieve a PendingIntent that will start a new activity, like calling
* {@link Context#startActivity(Intent) Context.startActivity(Intent)}.
* Note that the activity will be started outside of the context of an
* existing activity, so you must use the {@link Intent#FLAG_ACTIVITY_NEW_TASK
* Intent.FLAG_ACTIVITY_NEW_TASK} launch flag in the Intent.
...
* @return Returns an existing or new PendingIntent matching the given
* parameters. May return null only if {@link #FLAG_NO_CREATE} has been
* supplied.
消的PendingIntent只能
下面是取消的样子:
/**
* Cancel a currently active PendingIntent. Only the original application
* owning an PendingIntent can cancel it.
*/
public void cancel() {
try {
ActivityManagerNative.getDefault().cancelIntentSender(mTarget);
} catch (RemoteException e) {
}
}
该文件指出,应用程序可以取消的PendingIntent为您服务。本质上,这意味着ActivityManager尝试匹配的PendingIntent和删除条件的元数据/高速缓存条目的匹配的PendingIntent存在
The documentation states that the application can cancel the PendingIntent for you. Essentially, it means that ActivityManager attempts to match the PendingIntent and remove the meta data /cache entry on condition that the matching PendingIntent exists.
如果您尝试获得previously取消或应用了标志FLAG_NO_CREATE未注册的PendingIntent,则返回一个空呢。
If you attempt to get the previously cancelled or unregistered PendingIntent with the flag FLAG_NO_CREATE applied, a null is returned then.
通过AlarmManager取消的PendingIntent
通过AlarmManager取消,因为它消除注册的PendingIntent在IAlarmManager缓存/元数据文件,从我注意到潜水深入到Android源$ C $ C有没有取消正在通过ActivityManager完成后报警被删除明显不同。
Cancellation via AlarmManager differs obviously because it removes registered PendingIntent in IAlarmManager's cache/meta data files and from what I noticed diving deeper into Android source code there is no cancellation being done via ActivityManager when an alarm is removed.
public void cancel(PendingIntent operation) {
try {
mService.remove(operation); IAlarmManager instance
} catch (RemoteException ex) {
}
}
结论
一旦你注册,取消的PendingIntent本身和AlarmManager的警报取消过程没有任何共同之处您必须取消通过AlarmManager你的闹钟。
You must cancel your alarm via AlarmManager once you register it, cancelling PendingIntent itself and the AlarmManager's alarms cancellation process have nothing in common.
希望,我澄清你的疑惑。
Hope, that I clarified your doubts.
这篇关于PendingIntent.cancel()和AlarmManager.cancel的使用()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!