سؤال

I have widget that start 2 services using AlaramManager and running them repeatedly:

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); 


  }
}

After i delete Widget from the screen i am cancelling the services:

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


} 

But the services not cancelling and keep running in background. What can be the problem?

هل كانت مفيدة؟

المحلول

Cancelling the alarms won't stop the services. You have to either stop them manually, or have time stop themselves after they finish their work.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top