5

我已经阅读了 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);

我的取消警报功能

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

我的时代服务

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

    }

}
4

3 回答 3

2

好吧,要取消警报,您必须创建与启动它时创建的相同的 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);

他们是一样的吗?

不,它们不一样。您正在创建PendingIntentforService开始并尝试使用不同PendingIntent的取消BroadCast

于 2012-08-25T04:27:05.920 回答
1

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

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

于 2012-08-24T05:33:15.957 回答
0

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

@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()。

请参阅开发人员文档中的示例:

安卓服务

我在我的应用程序音频控制中使用了类似的东西,它运行良好。

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

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

于 2012-08-24T15:11:15.703 回答