【问题标题】:Alarm Pending Alarm doesn't cancel警报未决警报不取消
【发布时间】:2012-08-21 15:47:47
【问题描述】:

我在 Stackoverflow 上阅读了许多问题和答案,其中许多只是强调 .cancel() 和特殊的唯一 ID。但是,现在无论我尝试了多少次,我都无法取消它。


我的唯一 ID

final static int RQS_1 = 1337;


我的 setAlarm 函数。 pickTime 是当前的 Activity,timesUp 是另一个 Service 类,它在时间到时显示一个 toast。

Intent intent = new Intent(pickTime.this, timesUp.class);
PendingIntent timesUpIntent = PendingIntent.getService(pickTime.this, RQS_1, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(),
                 timesUpIntent);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), timesUpIntent);

我的 cancelAlarm 函数

Intent intent = new Intent(this, pickTime.class);
PendingIntent timesUpIntent = PendingIntent.getBroadcast(this, RQS_1, intent, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        if (timesUpIntent != null) {
            alarmManager.cancel(timesUpIntent);
            timesUpIntent.cancel();
            Toast.makeText(getApplicationContext(), "Alarm is cancelled",
                    Toast.LENGTH_SHORT).show();
                    } else {

            Toast.makeText(getApplicationContext(), "Unable to stop timer",
                    Toast.LENGTH_SHORT).show();
        }

My timesUp 服务

public class timesUp extends Service {

    @Override
    public void onCreate() {

        // TODO Auto-generated method stub

        Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG)
                .show();

    }

    @Override
    public IBinder onBind(Intent intent) {

        // TODO Auto-generated method stub

        Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG)
                .show();

        return null;

    }

    @Override
    public void onDestroy() {

        // TODO Auto-generated method stub

        super.onDestroy();

        Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG)
                .show();

    }

    @Override
    public void onStart(Intent intent, int startId) {

        // TODO Auto-generated method stub

        super.onStart(intent, startId);

        Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG)
                .show();

    }

    @Override
    public boolean onUnbind(Intent intent) {

        // TODO Auto-generated method stub

        Toast.makeText(this, "MyAlarmService.onUnbind()", Toast.LENGTH_LONG)
                .show();

        return super.onUnbind(intent);

    }

}

【问题讨论】:

    标签: android android-pendingintent


    【解决方案1】:

    要取消警报,您必须创建与启动它时创建的相同的 PendingIntent。 你正在做的同时开始,

    Intent intent = new Intent(pickTime.this, timesUp.class);
    PendingIntent timesUpIntent = PendingIntent
                                    .getService(pickTime.this, RQS_1, intent, 0);
    

    您正在取消,

    Intent intent = new Intent(this, pickTime.class);
    PendingIntent timesUpIntent = PendingIntent
                                            .getBroadcast(this, RQS_1, intent, 0);
    

    它们是一样的吗?

    不,它们不一样。您正在为 Service 创建 PendingIntent 以启动并尝试使用不同的 PendingIntentBroadCast 取消。

    【讨论】:

      【解决方案2】:

      我怀疑您的代码中方法 PendingIntent.getService(pickTime.this, RQS_1, intent, 0); 的最后一个(第 4 个)参数有问题。

      尝试使用PendingIntent.FLAG_CANCEL_CURRENT而不是0,它应该可以工作。

      【讨论】:

        【解决方案3】:

        不确定这是不是唯一的问题,但是

        @Override
        public void onStart(Intent intent, int startId) {
        
            // TODO Auto-generated method stub
        
            super.onStart(intent, startId);
        
            Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG)
                    .show();
        
        }**strong text**
        

        不推荐使用 onStart。对于服务使用 onStartCommand()。

        查看开发者文档中的示例:

        Android Services

        我在我的应用音频控制中使用了类似的东西,效果很好。

        Intent intent = new Intent(getApplicationContext(), QuickReceiver.class);
        intent.putExtra("extraKeyHere", "some extra if you like");
        
        PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(),
            1137, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        
        AlarmManager al = 
            (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
        
        al.setRepeating(AlarmManager.RTC_WAKEUP, endCal.getTimeInMillis(), 
            AlarmManager.INTERVAL_DAY, pi);
        

        这只是创建一个将触发广播接收器 (QuickReceiver.class) 的意图。

        public class QuickReceiver extends BroadcastReceiver
        {   
        @Override
        public void onReceive(Context context, Intent intent)
        {   
                Bundle extras = intent.getExtras();
                if(extras != null)
                {   
                String endProfile = extras.getString("extraKeyHere");
        
                    Intent i = new Intent(context, QuickReceiver.class);
        
                    PendingIntent pi = PendingIntent.getBroadcast(context,     
                    1137, i, PendingIntent.FLAG_CANCEL_CURRENT);
        
                    AlarmManager al =                                       
                         (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        
                    al.cancel(pi);
                }
            }
        }
        

        请注意,我对两个 pendingIntent 都使用了“getBroadcast()”。您可以从广播开始您的服务(当然使用 onStartCommand() :))并在那里做更多的工作。

        【讨论】:

        • 很抱歉,我正在尝试找到取消闹钟的方法。无论如何,谢谢。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多