【问题标题】:Android service doesn't restartAndroid 服务没有重启
【发布时间】:2018-03-15 01:08:19
【问题描述】:

应用停止时我的服务没有重新启动。我的错误在哪里? 我的安卓版本是 7.1.1 我试试看; 我在 onStartCommand 上返回了 START_STICKY 。 我写了这个方法。

@Override
    public void onTaskRemoved(Intent rootIntent){
        Log.e("Tem Harita","Service Killed");
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            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() + 3500,
                    restartServicePendingIntent);
            super.onTaskRemoved(rootIntent);
        }

    }

但有时有效,有时无效。我不知道为什么。

【问题讨论】:

  • 有时候问题有点不清楚
  • 是的,我知道为什么它不起作用。我只是问:(

标签: android android-service android-7.0-nougat android-version


【解决方案1】:

转到您的手机 设置->电池->电池优化->点击你的应用->点击不优化

您可以对所有应用执行相同操作

【讨论】:

    猜你喜欢
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 2014-01-21
    • 2016-12-05
    • 2016-05-06
    • 2017-05-09
    • 2018-07-10
    • 2021-11-20
    相关资源
    最近更新 更多