Using PendingIntent.cancel () and AlarmManager.cancel ()

How PendingIntent.cancel () affects the AlarmManager if there is a pending alarm.

Should I cancel the cancel on both objects (intent and alarm) to cancel the alarm? Can someone explain how they work together.

Thanks in advance.

0
source share
1 answer

Register PendingIntents

A PendingIntent instance can be obtained using the factory methods PendingIntent.getActivity (), PendingIntent.getService (), PendingIntent.getBroadcast ().

, PendingIntent, ActivityManager PendingIntent cache/meta data, . , , .

,

 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.

PendingIntent FLAG_NO_CREATE, null.

PendingIntent AlarmManager

AlarmManager , , , PendingIntent / IAlarmManager , , Android, ActivityManager.

public void cancel(PendingIntent operation) {
    try {
        mService.remove(operation); IAlarmManager instance
    } catch (RemoteException ex) {
    }
}

AlarmManager , PendingIntent, AlarmManager .

, .

+2

All Articles