【问题标题】:Service stop's whenever app is killed每当应用程序被杀死时服务停止
【发布时间】:2018-06-08 13:07:45
【问题描述】:

START_STICKY 在我杀死我的应用程序时在我的设备中不起作用然后服务不会再次启动,我的设备名称是 Redmi Note 3 Pro,但是每当我在 android 模拟器中运行相同的应用程序时,它会在我杀死时重新启动服务应用程序和服务不会停止,直到我通过 stopService() 方法停止它

请帮帮我

问题已解决

这样做:

设置>权限>自动启动 然后打开我的应用的开关,完成!

我在这个链接中得到了解决方案:Solution Link

【问题讨论】:

  • 使用ServiceConnection和服务绑定来保持服务而不破坏
  • @HemanthSTobi ok 会尝试的,感谢您的帮助,但感谢它

标签: android android-studio android-service


【解决方案1】:

你需要在服务属性里面的manifest中添加“android:process=:any_name”。

例如,

      <service android:name=".MyService"
        android:process=":MyService"
        android:enabled="true"/>

下面是我的 Service 类的代码。

public class MyService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}


@Override
public void onCreate() {
    super.onCreate();
    Log.e("onCreate", "onCreate");
    Toast.makeText(AppController.getInstance(),"Created",Toast.LENGTH_SHORT).show();
}

@Override
public void onDestroy() {
    super.onDestroy();
    Log.e("servicedestroy", "servicedestroy");
    Toast.makeText(AppController.getInstance(),"service destroy",Toast.LENGTH_SHORT).show();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Timer t = new Timer();
    t.scheduleAtFixedRate(new TimerTask() {
                              @Override
                              public void run() {

                                  new Handler(Looper.getMainLooper()).post(new Runnable() {
                                      @Override
                                      public void run() {
                                          Toast.makeText(AppController.getInstance(),"Running",Toast.LENGTH_SHORT).show();
                                      }
                                  });


                              }

                          },
            0,
            5000);

    return START_STICKY;
}

@Override
public void onTaskRemoved(Intent rootIntent) {
    Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());
    restartServiceIntent.setPackage(getPackageName());

    PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);
    AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    alarmService.set(
            AlarmManager.ELAPSED_REALTIME,
            SystemClock.elapsedRealtime() + 1000,
            restartServicePendingIntent);

    super.onTaskRemoved(rootIntent);
}

}

【讨论】:

  • 是的,我知道这可以通过在 onTaskRemoved 或 onDestroy 中使用 AlarmManager 来实现,只是忘了在这里分享,非常感谢您的回答
  • 它对我不起作用,进程名称只是一个随机名称吗?!
  • @Keivan.k 是的,可以根据您的选择使用任何名称..
  • 当我删除应用程序时,我在 Redmi 手机上检查了这一点,前台服务被杀死并在 5 秒后启动前台服务,但是当我再次打开应用程序并从最近再次清除时,它没有启动前台服务。有什么解决办法吗?
  • @AnwarZahid 您在清单中正确提到了 Service 类吗?
【解决方案2】:

在某些设备(尤其是小米、华为、联想)上,您需要将您的应用添加到“受保护的应用”或“允许在后台运行的应用”列表中。如果您的应用不在列表中,Android 不会自动重启您的Service,即使您已从onStartCommand() 返回START_STICKY。这是一个“省电功能”,不幸的是给开发者带来了很多问题!

在电源管理、安全或应用下的 Android 设置中查找这些设置。

另见:

还请解释一下“杀死我的应用”是什么意思。如果你强制关闭你的应用程序,那么Service 将不会被 Android 重新启动。这是故意的,如果您强制关闭应用程序,它也不会在模拟器上重新启动。

【讨论】:

  • 感谢工作,我已经通过 setting >Permissions>Autostart 启用了自动启动功能,我在这个链接中找到了解决方案:Solution Link
  • 前台服务呢?
  • @MuhammadBabar 我不明白你的问题。通常Android不会杀死前台Service,但在这种情况下,我假设如果Android杀死你的前台Service(无论出于何种原因),它仍然不会重新启动它,除非你的应用程序被添加到这个列表中应用程序。
  • @DavidWasser 谢谢,你回答了我的问题。
【解决方案3】:

代码可能会帮助您在HourlyServiceonStartCommand 方法返回START_STICKY

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return START_STICKY;
}

内部活动

 private HourlyService mHourlyService;
private ServiceConnection mServiceConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
        // We've bound to LocalService, cast the IBinder and get LocalService instance
        LocalBinder binder = (LocalBinder) iBinder;
        mHourlyService = binder.getService();
        mBound = true;
    }

    @Override
    public void onServiceDisconnected(ComponentName componentName) {
        mBound = false;
    }
};

@Override
protected void onStart() {
    super.onStart();
    // Bind to LocalService
    Intent intent = new Intent(this, HourlyService.class);
    bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
}

@Override
protected void onStop() {
    super.onStop();
    unbindService(mServiceConnection);
    mBound = false;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多