Android IntentService只执行一次
我正在使用Android AlarmManger计划一个IntentService在小时间间隔后运行 这是我的闹钟管理器:Android IntentService只执行一次,android,intentservice,Android,Intentservice,我正在使用Android AlarmManger计划一个IntentService在小时间间隔后运行 这是我的闹钟管理器: static void setNextSchedule(Context ctx, long interval){ AlarmManager manager = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE); Intent nIntent = new Intent(ctx,
static void setNextSchedule(Context ctx, long interval){
AlarmManager manager = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
Intent nIntent = new Intent(ctx, DService.class);
PendingIntent pIntent = PendingIntent.getActivity(ctx, 1, nIntent, PendingIntent.FLAG_UPDATE_CURRENT);
manager.setRepeating(AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + interval, interval, pIntent);
Log.i(ScheduleAlarm.class.getSimpleName(), "Next alarm set");
}
以及我的意向服务:
@Override
protected void onHandleIntent(Intent intent) {
Log.i(DService.class.getSimpleName(), "Service starting");
ScheduleAlarm.setNextSchedule(getApplicationContext(), 15000);
}
目前它只保存用于测试的代码现在,一旦应用程序启动,此代码将成功运行,不会出现任何问题,但15秒后,它将在logcat中声明服务正在启动,但代码不会执行
如何让INTERNET服务在每次运行时执行OnHandleContent
中的代码
感谢当使用
悬挂式组件时,出厂方法需要与您的意图所针对的组件类型相匹配:
- 如果您的
意图
指向某个活动,请使用getActivity()
- 如果您的
意图
指向服务,请使用getService()
- 如果您的
意图
指向广播接收器
,请使用getBroadcast()
- 如果您的
意图
没有指向上述任何一项,请直接入狱,不要越狱!,不收取$200:-)
“但代码不会执行”——什么是“代码”?您是如何确定它“不会执行”的?为什么对名为DService
?@commonware的类使用getActivity()
pendingent
作为意图
?@commonware-您是正确的,更改为getService()
,现在一切正常。加上我会接受的答案。非常感谢。