2017-01-10 74 views
1

我有一個代碼,其中警報管理器啓動服務。我必須使用第二個鬧鐘在指定時間內取消它。我看過的解決方案並不是單一的。我的代碼如下:無法使用Android中的AlarmManager停止服務

void startAtInterval(int fromTime, int fromTimeMinute, int toTime, int toTimeMinute, int id1, int id2) { 
    // start alarm 

    AlarmManager alarmMgr = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE); 
    PendingIntent alarmIntent = PendingIntent.getService(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

    Calendar calendar = Calendar.getInstance(); 
    calendar.setTimeInMillis(System.currentTimeMillis()); 
    calendar.set(Calendar.HOUR_OF_DAY, fromTime); 
    calendar.set(Calendar.MINUTE, fromTimeMinute); 

    alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 
      AlarmManager.INTERVAL_DAY, alarmIntent); 

    // stop alarm 

    AlarmManager alarmMgr1 = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE); 
    PendingIntent alarmIntent1 = PendingIntent.getService(getApplicationContext(), 1, intent, PendingIntent.FLAG_CANCEL_CURRENT); 

    Calendar calendar1 = Calendar.getInstance(); 
    calendar1.setTimeInMillis(System.currentTimeMillis()); 
    calendar1.set(Calendar.HOUR_OF_DAY, toTime); 
    calendar1.set(Calendar.MINUTE, toTimeMinute); 

    alarmMgr1.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar1.getTimeInMillis(), 
      AlarmManager.INTERVAL_DAY, alarmIntent1); 
    stopService(intent); 

     alarmMgr1.cancel(alarmIntent1); 

} 

我用FLAG_UPDATE_CURRENTFLAG_CANCEL_CURRENT。我也試圖stopservice,因爲我在我的代碼中顯示。我從配置屏幕傳遞時間。我知道它是有效的,因爲第一次警報總是被解僱。

回答

0

要停止你必須使用這個任何告警管理:

Intent intentS = new Intent(ctx,YourService.class); 
      intentS.addCategory("SOMESTRING_AS_TAG"); 
      PendingIntent senderS = PendingIntent.getService(ctx, NDX, intentS, PendingIntent.FLAG_CANCEL_CURRENT); 
      am.cancel(senderS); 
      senderS.cancel(); 

    //and this 
     PendingIntent senderSNew = PendingIntent.getService(ctx, NDX, intentS, 0); 
     am.cancel(senderSNew); 
     senderSNew.cancel(); 

其中NDX是一樣的開始。你的情況就是0。或者,更好的辦法是將您的NDX更改爲某個隨機數字。

停止:

Intent intent = new Intent(ctx,YourService.class); 
    intent.addCategory("SOMESTRING_AS_TAG"); 
    ctx.stopService(intent); 
+0

當我使用相同的NDX在兩個報警器(啓動和停止)第一永遠不會被解僱。爲了防萬一,我做了另一次嘗試,但沒有奏效。但是,如果我改變數字,首先會被解僱。 –

+0

@NachoC。 ,第一個是第一個。那麼,它會被取消嗎? – Vyacheslav

+0

@NachoC。 ,更新了一點 – Vyacheslav

0

你應該有它取消該服務火災BroadcastReceiver,然後停止服務的第二警報。這將確保警報將在任何情況下成功停止服務,例如關閉應用程序。

AlarmManager alarmMgr1 = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE); 
Intent intentCancelService= new Intent(getApplicationContext(), StopServiceReceiver.class); 
PendingIntent alarmIntent1 = PendingIntent.getBroadcast(getApplicationContext(), StopServiceReceiver.REQUEST_CODE, intentCancelService, PendingIntent.GoTestAlarmReceiver); 

Calendar calendar1 = Calendar.getInstance(); 
calendar1.setTimeInMillis(System.currentTimeMillis()); 
calendar1.set(Calendar.HOUR_OF_DAY, toTime); 
calendar1.set(Calendar.MINUTE, toTimeMinute); 

alarmMgr1.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar1.getTimeInMillis(), 
     AlarmManager.INTERVAL_DAY, alarmIntent1); 

然後讓你的BroadcastReceiver

public class GoTestAlarmReceiver extends BroadcastReceiver { 
    public static final int REQUEST_CODE = 123123; //whatever code just unique 

@Override 
public void onReceive(Context context, Intent intent) { 
    Intent i = new Intent(context, YourServiceClassHere.class); 
    context.stopService(i); 
    } 
} 

確保申報接收器在您的清單:

<receiver 
     android:name=".StopServiceReceiver" 
     android:enabled="true" 
     android:process=":remote" /> 
+0

你確定PendingIntent.GoTestAlarmReceiver在getBroadcast()方法中嗎?它由編譯器P.加下劃線。我是Android新手,無法理解應該在那裏放置什麼 –

+0

現在我粘貼了PendingIntent.FLAG_CANCEL_CURRENT –