我有一个小部件,它使用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);
}
}
我从屏幕上删除Widget后,我取消了服务:
@Override
public void onDisabled(Context context)
{
final AlarmManager m = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
m.cancel(serviceShowNextSubject);
m.cancel(serviceDownloadSubjects);
}
但服务不会取消并继续在后台运行。 可能是什么问题?
答案 0 :(得分:1)
取消警报不会停止服务。您必须手动停止它们,或者在完成工作后有时间停止它们。