有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法在Android中使用AlarmManager停止服务

我有一个alarm manager启动服务的代码。我必须用第二个闹钟在规定时间内取消它。我所看到的任何解决方案都不起作用。我的代码如下:

 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如我在代码中所示。我正在通过配置屏幕传递时间。我知道它是有效的,因为第一个警报总是被触发的


共 (2) 个答案

  1. # 1 楼答案

    要停止任何报警管理器,必须使用以下命令:

    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与start相同。这就是0在你的情况下。或者,更好的是,将NDX更改为某个随机常数

    要停止:

    Intent intent = new Intent(ctx,YourService.class);
        intent.addCategory("SOMESTRING_AS_TAG");
        ctx.stopService(intent);
    
  2. # 2 楼答案

    你应该有第二个警报,取消服务,然后停止服务。这将确保警报在任何情况下都能成功停止服务,例如关闭应用程序

    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" />