Android的AlarmManager.cancel()实际上没有取消报警/的PendingIntent [英] Android AlarmManager.cancel() not actually cancelling alarm/PendingIntent

查看:2608
本文介绍了Android的AlarmManager.cancel()实际上没有取消报警/的PendingIntent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图取消AlarmManager报警,倒不正常工作。

Trying to cancel an AlarmManager alarm, isn't quite working.

我创建的PendingIntent像这样:

I create a PendingIntent like so:

static PendingIntent makePendingIntent(int id, Bundle bundle)
{
    Intent intent = new Intent(mContext.getApplicationContext(), mfLocalNotificationManager.class);
    if(bundle != null)
    {
        intent.putExtra(BUNDLE_ID, bundle);
    }

    return PendingIntent.getBroadcast(mContext.getApplicationContext(), id, intent, PendingIntent.FLAG_UPDATE_CURRENT);

}

这SendLocalNotification调用:

Called from SendLocalNotification:

public static int SendLocalNotification(String title, String text, String tickerText, 
        int timeSent, int timeOffset, String sound)
{

    AlarmManager alarmMgr = 
        (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);   

    Bundle notificationData = new Bundle();
    notificationData.putString("title", title);
    notificationData.putString("text", text);
    notificationData.putString("tickerText", tickerText);
    /*snip, bundle stuff*/

    int noteID = title.hashCode();        

    notificationData.putInt("noteID", noteID);

    PendingIntent pendingIntent = makePendingIntent(noteID, notificationData);

    if( pendingIntent == null )
    {
            //This should probably be flagged as an error or an assertion. 
        Log.d("[MF_LOG]", "Java intent is null");
            return -1;
    }

    //This isn't my timing code, don't hate me for it
    Calendar time = Calendar.getInstance();
    time.setTimeInMillis(System.currentTimeMillis());
    time.add(Calendar.SECOND, timeOffset);

    alarmMgr.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(),
                 pendingIntent);

return noteID;
}

和尝试取消它像这样(通过我们previously得到了SendLocalNotification后面的ID):

And attempt to cancel it like so (passing the id we previously got back from SendLocalNotification):

public static void CancelNotification(int id)
{
    String ns = Context.NOTIFICATION_SERVICE;

    AlarmManager alarmMgr = 
            (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);

    PendingIntent pendingIntent = makePendingIntent(id, null);

    //This doesn't work...
    pendingIntent.cancel(); //<- added based on SO post, doesn't help
    alarmMgr.cancel(pendingIntent);
}

似乎没有任何工作,我也跟着上的其他职位尽可能多的意见,因为我能找到(确保该ID是一样的,保证的PendingIntent重新创建完全一样的),它似乎仍然崩溃。作为一个侧面说明,试图检查是否存在使用通知旗PendingIntent.FLAG_NO_CREATE也不起作用,创建一个新的对象,而不是返回空的。

Nothing seems to work, and I've followed as much of the advice on other posts as I can find (making sure the ID is the same, ensuring the PendingIntent is recreated exactly the same) and it still seems to crash. As a side note, trying to check if a notification exists using the flag PendingIntent.FLAG_NO_CREATE also doesn't work, creating a new object instead of returning null.

我在测试这个设备是一台Nexus 7和我建立针对API 9。

The device I'm testing this on is a Nexus 7 and I'm building against API 9.

推荐答案

您需要重新创建你的意图,并取消它是这样的:

You need to recreate your intent and CANCEL it like this:

AlarmManager alarm = (AlarmManager) context.getSystemService(ALARM_SERVICE);
PendingIntent peding = PendingIntent.getActivity(context, code, intent,     
                                                 PendingIntent.FLAG_CANCEL_CURRENT);
alarm.cancel(peding);

这篇关于Android的AlarmManager.cancel()实际上没有取消报警/的PendingIntent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆