Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从服务中停止AlarmManager,但已在“活动”中创建_Android_Service_Alarmmanager - Fatal编程技术网

Android 从服务中停止AlarmManager,但已在“活动”中创建

Android 从服务中停止AlarmManager,但已在“活动”中创建,android,service,alarmmanager,Android,Service,Alarmmanager,我按照以下方式创建AlarmManager。特定时间后,服务将使用stopSelf()停止,但alarmmanager将再次启动服务。 如何在服务中停止AlarmManager?活动不再运行,我必须在服务内部执行 使用AlarmManager定期启动服务的活动: Intent i=new Intent(this, AppService.class); PendingIntent pi = PendingIntent.getService(this, 0, i, PendingIntent.FLA

我按照以下方式创建AlarmManager。特定时间后,服务将使用stopSelf()停止,但alarmmanager将再次启动服务。 如何在服务中停止AlarmManager?活动不再运行,我必须在服务内部执行

使用AlarmManager定期启动服务的活动:

Intent i=new Intent(this, AppService.class);
PendingIntent pi = PendingIntent.getService(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 

if((tb_runBG.isChecked()))
{    
    alarmManager.cancel(pi);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 *update_time, pi);                              
}
else
  {    
    // From Activity it's no problem to stop the AlarmManager   
    alarmManager.cancel(pi);
    stopService(new Intent(this, AppService.class));                     
  }
Intent i=new Intent(this, volumeActivity.class);                      
PendingIntent pi=PendingIntent.getActivity(this, 0, i, 0);
// Her I should get the getSystemService from the Activity, but how??
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
// Following unfortunately not working ...
alarmManager.cancel(pi);
stopSelf(); 
尝试停止AlarmManager的服务:

Intent i=new Intent(this, AppService.class);
PendingIntent pi = PendingIntent.getService(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 

if((tb_runBG.isChecked()))
{    
    alarmManager.cancel(pi);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 *update_time, pi);                              
}
else
  {    
    // From Activity it's no problem to stop the AlarmManager   
    alarmManager.cancel(pi);
    stopService(new Intent(this, AppService.class));                     
  }
Intent i=new Intent(this, volumeActivity.class);                      
PendingIntent pi=PendingIntent.getActivity(this, 0, i, 0);
// Her I should get the getSystemService from the Activity, but how??
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
// Following unfortunately not working ...
alarmManager.cancel(pi);
stopSelf(); 
不幸的是,它不起作用。服务停止,但AlarmManager再次调用它

编辑:

以下各项也不起作用:

AlarmManager alarmManager = (AlarmManager) this.getBaseContext().getSystemService(Context.ALARM_SERVICE); 


我想在设置闹钟和取消闹钟的情况下,挂起的内容应该是相同的。在您的情况下,第二个挂起的意图与第一个不同。您能否在“取消”代码中尝试以下操作

    PendingIntent pi = PendingIntent.getService(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);

编辑:刚刚注意到,你的意图也不一样。这是故意的吗

非常感谢!!这不是故意的不同意图。以同样的意图和悬而未决的工作现在@user1390816它是如何工作的在我的例子中它在模拟器中工作,但在实际设备中不工作。