简体   繁体   English

警报挂起警报不会取消

[英]Alarm Pending Alarm doesn't cancel

I've read through many questions and answers on Stackoverflow, and many of which just emphasize on the .cancel() and the special unique ID. 我已经阅读了Stackoverflow上的许多问题和答案,其中许多只是强调.cancel()和特殊的唯一ID。 However, now matter how many times I tried, I just can't cancel it. 但是,现在无论我尝试了多少次,我都无法取消它。


My unique ID 我唯一的身份证

final static int RQS_1 = 1337;


My setAlarm Function. 我的setAlarm函数。 pickTime is current Activity, and timesUp, is another Service class that shows a toast when the time is up. pickTime是当前的Activity,timesUp是另一个在时间到期时显示toast的Service类。

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);

My cancelAlarm function 我的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 Service 我的时间服务

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);

    }

}

Well for cancelling Alarm you have to create the same PendingIntent that you created while starting it. 要取消警报,您必须创建在启动时创建的相同PendingIntent。 You are doing while starting, 你正在做的时候,

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

You are doing while cancelling, 你正在取消,

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

Are they same? 它们一样吗?

No, they are not same. 不,他们不一样。 You are creating PendingIntent for Service to start and trying to cancel with different PendingIntent of BroadCast . 您正在创建PendingIntentService启动,并试图用不同的取消PendingIntentBroadCast

I suspect there is something wrong with the last(4th) argument of the method PendingIntent.getService(pickTime.this, RQS_1, intent, 0); 我怀疑方法PendingIntent.getService(pickTime.this, RQS_1, intent, 0);的最后一个(第四个)参数有问题PendingIntent.getService(pickTime.this, RQS_1, intent, 0); in your code. 在你的代码中。

Try using PendingIntent.FLAG_CANCEL_CURRENT instead of 0, it should work. 尝试使用PendingIntent.FLAG_CANCEL_CURRENT而不是0,它应该工作。

Not sure if this is the only problem but 不确定这是否是唯一的问题但是

@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**

The onStart is deprecated. onStart已弃用。 For a service use onStartCommand(). 对于服务使用onStartCommand()。

See the example in the developer docs: 请参阅开发人员文档中的示例:

Android Services Android服务

I use something like this in my app Audio Control and it works well. 我在我的应用程序音频控制中使用这样的东西,它运作良好。

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);

This is just creating an intent that will trigger a broadcast receiver (QuickReceiver.class). 这只是创建一个触发广播接收器(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);
        }
    }
}

Make a note that I used "getBroadcast()" for both pendingIntents. 请注意我对两个pendingIntents使用了“getBroadcast()”。 You could start your service from the broadcast (using onStartCommand() of course :) ) and do more work there. 你可以从广播开始你的服务(当然使用onStartCommand(:))并在那里做更多的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM