cancelAlarm 方法实际上并没有取消我的 AlarmManager

cancelAlarm method not actually canceling my AlarmManager

我已经使用了堆栈上另一个有信誉的答案中的这个 cancelAlarm 方法,但它没有完成工作,而且我不知道为什么它不起作用。

我有一个 activity,其中一个按钮将启动一个警报,该警报将在每个给定的时间间隔内响起。然后我有另一个按钮可以取消该警报。首先是按钮:

    start.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            setupAlarm(10);
        }
    });

    stop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try{
                cancelAlarm(ALARM_ID);
            } catch(Exception e){
                Toast.makeText(getApplicationContext(), "ERROR", Toast.LENGTH_SHORT).show();
            }
        }
    });

这是我的 setupAlarmcancelAlarm 方法:

private void setupAlarm(int seconds) {
    AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
    Intent i = new Intent(getBaseContext(), AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(
            MainActivity.this, ALARM_ID, i, PendingIntent.FLAG_UPDATE_CURRENT);
    Calendar t = Calendar.getInstance();
    t.setTimeInMillis(System.currentTimeMillis());

    int interval = seconds*1000;
    am.setRepeating(AlarmManager.RTC_WAKEUP, t.getTimeInMillis(), interval, pendingIntent);
    MainActivity.this.finish();
}

private void cancelAlarm(int alarmId){
    Intent i = new Intent(getBaseContext(), AlarmManager.class);
    PendingIntent sender = PendingIntent.getBroadcast(MainActivity.this, alarmId, i, 0);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.cancel(sender);
    sender.cancel();
}

我记得用相同的 ID 制作了另一个警报管理器,并在 PendingIntent 上调用了 alarmManager.cancel(sender); 但它似乎没有做任何事情,因为我的服务无论如何都会重新启动。

报警接收器class:

public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent)
{
    Context oAppContext = context.getApplicationContext();

    if (oAppContext == null) {
        oAppContext = context;
    }

    Intent serviceIntent = new Intent(oAppContext, MyService.class);
    oAppContext.startService(serviceIntent);
}
}

我的服务class:

public class MyService extends Service implements SensorEventListener{

private PowerManager.WakeLock wakeLock;

Sensor mSensor;
SensorManager mSensorManager;
String toastString = "";

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate(){
    mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
    mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);

    PowerManager mgr = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
    wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
    wakeLock.acquire();


    Toast.makeText(this, "onCreate Successful", Toast.LENGTH_SHORT).show();
}

@Override
public int onStartCommand(Intent intent, int flags, int startid) {
    Toast.makeText(this, "onStart Successful", Toast.LENGTH_SHORT).show();
    return Service.START_STICKY;
}

@Override
public void onSensorChanged(SensorEvent event) {
    //record some data from the accelerometer
    quit();
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {

}

private void quit(){
    mSensorManager.unregisterListener(MyService.this);
    wakeLock.release();
    this.stopSelf();
}
}

而且我继续从我的 MyService class 那里得到祝酒词,告诉我我的 onCreateonStart 已成功实例化,即使在调用我的 cancelAlarm方法。

使用您在创建 Alarm 时使用的相同 PendingIntent。在 Intent:

中使用 AlarmReceiver.class 而不是 AlarmManager.class
Intent i = new Intent(getBaseContext(), AlarmReceiver.class);

更新 cancelAlarm() 方法如下:

private void cancelAlarm(int alarmId){
    Intent i = new Intent(getBaseContext(), AlarmReceiver.class);
    PendingIntent sender = PendingIntent.getBroadcast(MainActivity.this, alarmId, i, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.cancel(sender);
    sender.cancel();
}

希望对你有所帮助~