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

时间:2016-11-16 20:45:29

标签: android broadcast alarm receiver

我无法停止闹钟管理器或广播接收器。有什么想法解决这个问题吗?

非常感谢提前。顺便说一句,我使用开关按钮开启/关闭闹钟。

这是我的代码;

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

2 个答案:

答案 0 :(得分:0)

尝试使用取消警报按钮:



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或类似方法在终端中搜索警报不会显示实际警报,只会显示警报/唤醒的历史记录。

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

答案 1 :(得分:0)

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

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

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