警报管理器和广播接收器不停止

Alarm Manager&Broadcast receiver not stopping

我无法停止警报管理器或广播接收器。有解决这个问题的想法吗?

非常感谢。顺便说一句,我正在使用开关按钮作为警报 on/off。

这是我的代码;

MainActivity.class

public void setAlarm(Calendar alarmCalender)
{
    Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), REQUEST_CODE, intent, 0);
    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, alarmCalender.getTimeInMillis(), pendingIntent);

}

public void cancelAlarm()
{

    AlarmManager aManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
    PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    aManager.cancel(pIntent);
    stopService(intent);
}

切换按钮:

bolme1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean aktiflestirmisse)
            {
                if(aktiflestirmisse)
                {
                    setAlarm(calSet);
                    btn1.setBackgroundColor(getResources().getColor(R.color.aktifdurum));
                    Toast t= Toast.makeText(MainActivity.this,"Bölme 1 aktif hale getirildi.",Toast.LENGTH_SHORT);
                    t.show();
                }

                else
                {
                    cancelAlarm();
                    btn1.setBackgroundColor(getResources().getColor(R.color.pasifdurum));
                    Toast t = Toast.makeText(MainActivity.this, "Bölme 1 pasif hale getirildi.", Toast.LENGTH_SHORT);
                    t.show();
                }

无论如何这是我的AlarmReceiver.java

public class AlarmReceiver extends BroadcastReceiver {
NotificationManager manager;
private final int NOTIFICATION_ID =1;
@Override
public void onReceive(Context context, Intent intent) {

    NotificationManager mng=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification.Builder nb=new Notification.Builder(context);
    nb.setContentTitle("Hatırlatma");
    nb.setContentText("İlgili bölmenin vakti ilaç vakti geldi!");
    nb.setSmallIcon(R.drawable.alarm_uyari);
    nb.setTicker("Bildiriminiz var.");
    nb.setAutoCancel(true);
    PendingIntent pi=PendingIntent.getActivity(context,NOTIFICATION_ID,intent,0);
    nb.setContentIntent(pi);
    Notification not=nb.getNotification();
    not.vibrate=new long[]{500,500,500,500};
    mng.notify(NOTIFICATION_ID,not);

    Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
    if (alarmUri == null)
    {
        alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    }
    Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
    ringtone.play();
}}

试试这个取消闹钟按钮:

public void cancelAlarm() {
        Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
        final PendingIntent pIntent = PendingIntent.getBroadcast(this, AlarmReceiver.REQUEST_CODE,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager aManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
        aManager.cancel(pIntent);
}

不需要stopService;一旦您将 .cancel 传递给 AlarmManager,它将通过广播取消服务。要在终端 'adb shell dumpsys alarm' 中检查它是否被取消 运行 然后按你的包名搜索。有时使用 findstr 或类似工具在终端中搜索警报不会显示实际警报,而只会显示 alarms/wakeups 的历史记录。

让我知道这是否有效! :)

我将此代码用于我自己的应用程序:

创建待定意图并包装我们的意图:

PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
id, intent, PendingIntent.FLAG_UPDATE_CURRENT);

//get the alarm manager
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= 23)
{
        alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, date.getTime(), pendingIntent);
}
else if (android.os.Build.VERSION.SDK_INT >= 19)
{
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, date.getTime(), pendingIntent);
}
else alarmManager.set(AlarmManager.RTC_WAKEUP, date.getTime(), pendingIntent);

取消:

private void cancelAlarm(long id)
{
    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);

    Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
    PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(), (int)id, intent,  PendingIntent.FLAG_UPDATE_CURRENT);
    alarmManager.cancel(sender);
}

所以在你的情况下,这可能适用于设置闹钟

Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, alarmCalender.getTimeInMillis(), pendingIntent);