我在Stackoverflow上阅读了很多问题和答案,其中许多只是强调.cancel()
和特殊的唯一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();
}
我的时间服务
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);
}
}
答案 0 :(得分: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);
它们一样吗?
不,他们不一样。您正在为PendingIntent
创建Service
以开始尝试取消PendingIntent
BroadCast
个{{1}}。
答案 1 :(得分:1)
我怀疑代码中PendingIntent.getService(pickTime.this, RQS_1, intent, 0);
方法的最后一个(第四个)参数有问题。
尝试使用PendingIntent.FLAG_CANCEL_CURRENT
代替0,它应该可以使用。
答案 2 :(得分: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(:))并在那里做更多的工作。