我知道之前曾问过这个问题,但是我没有得到任何答案,我想创建一个一直运行线程的意图服务,但是当我从应用程序退出时,我的服务停止了,线程也停止了。我需要创建一些东西来每隔几分钟唤醒一次服务。或即使应用程序被终止或关闭也可以防止终止服务的措施。
这就是我开始服务的方式
Intent intent= new Intent(Intent.ACTION_SYNC,null,this,IntentServ.class);
startService(intent);
为此,您可以使用AlarmManager
可以每1小时启动一次服务的。例如 :
AlarmManager mgr = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent(context,
UpdateService.class);
PendingIntent pendingIntent=PendingIntent.getService(context, requestCode, Intent.parseIntent(), 0);
mgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis(), AlarmManager.INTERVAL_HOUR, pendingIntent);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句