【问题标题】:Android service not stopping after cancellingAndroid服务在取消后没有停止
【发布时间】:2012-06-12 07:56:11
【问题描述】:

我有一个使用 AlaramManager 启动 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); 


  }
}

从屏幕上删除小部件后,我将取消服务:

@Override  
public void onDisabled(Context context)  
{  
    final AlarmManager m = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);  
    m.cancel(serviceShowNextSubject);  
    m.cancel(serviceDownloadSubjects); 


} 

但服务没有取消并继续在后台运行。 可能是什么问题?

【问题讨论】:

    标签: android android-widget android-service alarmmanager repeatingalarm


    【解决方案1】:

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

    【讨论】:

    • 从屏幕上删除小部件时如何手动停止它们?
    • 你们的服务具体是做什么的?你能让他们在完成后停下来吗?这是更清洁的解决方案。
    • 第一个服务每 X 分钟从服务器下载一些数据,第二个服务每隔 X 秒用新数据刷新小部件,只有当小部件从屏幕上删除时才需要停止它们。
    • 不完全——如果您使用的是AlaramManager,您的下载服务将每X分钟启动一次。因此,您可以在下载后停止它。与其他服务相同 - 安排它每 X 秒运行一次(顺便说一句,即使对于小部件来说也是如此),并在完成刷新时停止它。
    • 我试图从内部阻止:stopSelf() 在服务本身,但它一直在运行。
    猜你喜欢
    • 1970-01-01
    • 2011-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-17
    • 2020-08-09
    • 2014-06-22
    相关资源
    最近更新 更多