在我的应用程序中,我在后台使用基于位置的服务。因此,我需要重新启动我的服务,当它被摧毁。
但我在logcat上收到了这条消息
20614:com.odoo.crm:my_odoo_gps_service/u0a391},{320 af6 curProc for 20614: null的伪死
我的服务onTaskRemoved
@Override
public void onTaskRemoved(Intent rootIntent) {
System.out.println("onTaskRemoved called");
Intent restartServiceIntent = new Intent(App.getAppContext(), this.getClass());
restartServiceIntent.setPackage(getPackageName());
PendingIntent restartServicePendingIntent =
PendingIntent.getService(App.getAppContext(), 1, restartServiceIntent,
PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmService =
(AlarmManager) App.getAppContext().getSystemService(Context.ALARM_SERVICE);
alarmService.set(
AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + 1000,
restartServicePendingIntent);
}我的服务onDestroy
@Override
public void onDestroy() {
System.out.println("destroy service");
super.onDestroy();
wakeLock.release();
}我的服务onStartCommand
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return Service.START_STICKY;
}我不知道是什么错误。我在google和stackoverflow中都搜索过。所有这些都提到了Service.START_STICKY.但我已经用过了。
在KitKat中,同样的服务重新启动工作,但是延迟了一些时间(~5分钟)。
任何帮助都是非常感谢的。
发布于 2018-06-23 12:39:35
最后,我在Evernote JobService的帮助下实现了
Github链接- https://github.com/evernote/android-job
步骤1:添加evernote作业服务依赖项
implementation 'com.evernote:android-job:1.3.0-alpha03'步骤2:创建DemoJobCreator.java类
public class DemoJobCreator implements JobCreator {
@Override
@Nullable
public Job create(@NonNull String tag) {
switch (tag) {
case DemoSyncJob.TAG:
return new DemoSyncJob();
default:
return null;
}
}
}步骤3:创建DemoSyncJob.java类
public class DemoSyncJob extends Job {
public static final String TAG = ">>>> job_demo_tag";
@Override
@NonNull
protected Result onRunJob(Params params) {
// run your job here
Log.d(TAG, "onRunJob: ");
if(!isMyServiceRunning(this.getContext(), TestService.class)){
Intent intent=new Intent(context,TestService.class);
context.startService(intent);
}
scheduleJob();
return Job.Result.SUCCESS;
}
public static void scheduleJob() {
new JobRequest.Builder(DemoSyncJob.TAG)
.setExecutionWindow(2_000L, 2_000L)
//.setPeriodic(900000) -> recommended. but it will work after 15 min (if you used this no need scheduleJob(); inside onRunJob();)
.build()
.schedule();
}
public static boolean isMyServiceRunning(Context context, Class<?> serviceClass) {
try {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
}catch (Exception e){
Log.e(TAG, "isMyServiceRunning: ",e );
}
return false;
}
}步骤4:在应用程序文件中(如果不可用,创建它),在onCreate()中添加以下行
JobManager.create(this).addJobCreator(new DemoJobCreator());步骤5:最后在活动中启动JobService
DemoSyncJob.scheduleJob();此JobService将检查服务运行与否(每2秒一次),如果服务不运行,它将重新启动服务。
免责声明:这可能不是正确的解决方案。但它将100%起作用。
我希望这至少对将来的任何人都有帮助。
发布于 2016-03-10 12:00:13
您可以使用BroadcasteReceiver重新启动它,它处理服务的onDestroy()发送的广播。
如何做到这一点:
StickyService.java
public class StickyService extends Service
{
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
sendBroadcast(new Intent("IWillStartAuto"));
}
@Override
public void onDestroy() {
super.onDestroy();
sendBroadcast(new Intent("IWillStartAuto"));
}
}RestartServiceReceiver.java
public class RestartServiceReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context.getApplicationContext(), StickyService.class));
}
}在清单文件中声明组件:
<service android:name=".StickyService" >
</service>
<receiver android:name=".RestartServiceReceiver" >
<intent-filter>
<action android:name="IWillStartAuto" >
</action>
</intent-filter>
</receiver>希望这能帮到你。
发布于 2016-03-12 11:28:23
onTaskRemoved中的代码阻止系统运行killProcess命令。Kitkat的延迟是由于使用alarmService.set造成的,它是API 19中的不准确,而是使用setExact。
如果您有一个想要保持活力的service,建议您将一个notification附加到它并使其成为foreground。这样,它被杀死的可能性就会降低。
https://stackoverflow.com/questions/35747624
复制相似问题