我無法停止報警管理器或廣播接收器。任何想法解決這個問題?報警管理器和廣播接收器不停止
非常感謝提前。順便說一下,我使用開關按鈕開啓/關閉鬧鐘。
這是我的代碼;
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();
}}
第一個塊是setAlarm的權利? –
是的,它也確保設備醒來的棉花糖,這是棘手的瞌睡 – PrisonMike
但我得到'ID'變量的錯誤。我的意思是,我要打電話給cancelAlarm(0)或(1)? –