2016-11-16 128 views
0

我無法停止報警管理器或廣播接收器。任何想法解決這個問題?報警管理器和廣播接收器不停止

非常感謝提前。順便說一下,我使用開關按鈕開啓/關閉鬧鐘。

這是我的代碼;

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

回答

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); 
} 
你的情況,這將

所以可能用於設置鬧鐘

​​
+0

第一個塊是setAlarm的權利? –

+0

是的,它也確保設備醒來的棉花糖,這是棘手的瞌睡 – PrisonMike

+0

但我得到'ID'變量的錯誤。我的意思是,我要打電話給cancelAlarm(0)或(1)? –

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;一旦你通過取消通過報警管理器,它將通過廣播取消服務。要檢查它是否被取消,請在終端的'adb shell dumpsys alarm'中運行,然後按您的軟件包名稱進行搜索。有時使用findstr或類似的方法來搜索終端中的鬧鐘將不會顯示實際鬧鐘,而只會顯示鬧鐘/喚醒的歷史記錄。

讓我知道這是否行得通! :)

+0

我想我應該在AlarmReceiver中定義public final int REQUEST_CODE = 1? –

+0

是的,你可以將它設置爲任何數字作爲標識 –

+0

任何數字?我應該定義final int REQUEST_CODE = 1還是0? –