2017-01-10 69 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 –