Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.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服务取消后未停止_Android_Android Widget_Android Service_Alarmmanager_Repeatingalarm - Fatal编程技术网

Android服务取消后未停止

Android服务取消后未停止,android,android-widget,android-service,alarmmanager,repeatingalarm,Android,Android Widget,Android Service,Alarmmanager,Repeatingalarm,我有一个小部件,它使用AlamManager启动2个服务并重复运行它们: public static final String TAG = "RotterWidgetProvider"; public static ArrayList<SubjectObject> subjectsList; public static boolean isUpdated=true; public static int currentSubject=0; private PendingIntent s

我有一个小部件,它使用AlamManager启动2个服务并重复运行它们:

public static final String TAG = "RotterWidgetProvider";
public static ArrayList<SubjectObject> subjectsList;
public static boolean isUpdated=true;
public static int currentSubject=0;
private PendingIntent serviceShowNextSubject = null;  
private PendingIntent serviceDownloadSubjects = null;
Intent i=null;
Intent i2=null;

@Override
public void onUpdate(final Context context, final AppWidgetManager appWidgetManager,int[] appWidgetIds) 
{
  RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.rotter_appwidget_layout);
  Intent launchAppIntent = new Intent(context, WidgetSettings.class);
  PendingIntent launchAppPendingIntent = PendingIntent.getActivity(context,
           0, launchAppIntent, PendingIntent.FLAG_UPDATE_CURRENT);
     remoteView.setOnClickPendingIntent(R.id.imgWidgetSettings, launchAppPendingIntent);
  if(subjectsList==null)
  {
     remoteView.setTextViewText(R.id.txtSubject,"נא לחכות, טוען נתונים");
     ComponentName RotterListWidget = new ComponentName(context,
                RotterWidgetProvider.class);
        appWidgetManager.updateAppWidget(RotterListWidget, remoteView);

       final AlarmManager m = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);  

        final Calendar TIME = Calendar.getInstance();  
        TIME.set(Calendar.MINUTE, 0);  
        TIME.set(Calendar.SECOND, 0);  
        TIME.set(Calendar.MILLISECOND, 0);  

        if(i==null)
        {
           i = new Intent(context, UpdateRotterWidgetService.class); 
           Log.v(TAG,"UpdateRotterWidgetService Created");
        }
        if(i2==null)
        {
           i2 = new Intent(context, DownloadSubjectsService.class); 
           Log.v(TAG,"DownloadSubjectsService Created");
        }

        if (serviceShowNextSubject == null)  
        {  
           serviceShowNextSubject = PendingIntent.getService(context, 0, i, PendingIntent.FLAG_CANCEL_CURRENT); 
           Log.v(TAG,"serviceShowNextSubject initialized");
        }  
        if(serviceDownloadSubjects==null)
        {
           serviceDownloadSubjects=PendingIntent.getService(context, 0, i2, PendingIntent.FLAG_CANCEL_CURRENT); 
           Log.v(TAG,"serviceDownloadSubjects initialized");
        }

        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
        String subjectsDownloadInterval=settings.getString("SubjectsDownloadInterval", "10");
        String subjectsUpdateInterval=settings.getString("SubjectsUpdateInterval", "10");
        int down=10;
        int upd=10;
        try
        {
           down=Integer.parseInt(subjectsDownloadInterval);
           upd=Integer.parseInt(subjectsUpdateInterval);
        }
        catch(Exception e)
        {

        }
        m.setRepeating(AlarmManager.RTC, TIME.getTime().getTime(), 1000 * 60*down, serviceDownloadSubjects);
        m.setRepeating(AlarmManager.RTC, TIME.getTime().getTime(), 1000 * upd, serviceShowNextSubject); 


  }
}
但这些服务不会取消,而是在后台继续运行。
有什么问题吗?

取消警报不会停止服务。您必须手动停止他们,或者在他们完成工作后让他们自己停止

当小部件从屏幕上移除时,我如何手动停止它们?您的服务具体做什么?你能让他们在完成后停下来吗?这是一个更干净的解决方案。第一个服务每X分钟从服务器下载一些数据,第二个服务每X秒用新数据刷新一次小部件,只有当小部件从屏幕上删除时才需要停止。不完全是这样——如果您使用的是AlaramManager,您的下载服务将每X分钟启动一次。所以你可以在下载后停止它。与另一个服务相同——计划它每X秒运行一次(顺便说一句,即使对于一个小部件来说,这也太多了),并在完成刷新时停止它。
@Override  
public void onDisabled(Context context)  
{  
    final AlarmManager m = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);  
    m.cancel(serviceShowNextSubject);  
    m.cancel(serviceDownloadSubjects); 


}