【问题标题】:Android: keep Service running when app is killedAndroid:当应用程序被杀死时保持服务运行
【发布时间】:2015-08-12 02:18:26
【问题描述】:

我想让IntentService 在后台运行,即使应用程序被终止。 “杀死”是指长时间按下主页按钮 -> 查看所有正在运行的应用程序 -> 将我的应用程序滑到一边 -> app 被杀 或者 长按返回键 -> app被杀

我的代码如下。在我的 MainActivity 中:

Intent intent = new Intent(this, MyService.class);
this.startService(intent);

在我的 MyService 中:

public class MyService extends IntentService {

@Override
protected void onHandleIntent(Intent intent) {
    System.out.println("MyService started");
    run();
}

private void run() {
    while (true){
        System.out.println("MyService still running");
        doSomething();
        waitSomeTime();
    }
}

}

当应用程序打开时,我看到服务正在运行。当我通过主页按钮最小化应用程序时,它仍在运行。当我通过后退按钮关闭应用程序时,它仍在运行。但是如果我如上所述杀死它,它将停止。我该如何解决?

【问题讨论】:

标签: android service background intentservice


【解决方案1】:

所有答案似乎正确,所以我将继续并在此处给出完整答案。

首先,最简单的方法是在手动杀死 应用程序时在 Android 中启动 广播,然后定义一个自定义 BroadcastReceiver 触发服务重启。

现在让我们进入代码。


YourService.java中创建您的服务

请注意onCreate() 方法,在该方法中,对于高于Android Oreo 的构建版本,我们以不同的方式启动前台服务。这是因为最近引入了严格的通知政策,我们必须定义自己的通知渠道才能正确显示它们。

onDestroy() 方法中的this.sendBroadcast(broadcastIntent); 是异步发送动作名称为"restartservice" 的广播的语句。稍后我们将使用它作为重启服务的触发器。

这里我们定义了一个简单的 Timer 任务,它在 Log 中每 1 秒 打印一个计数器值,并在每次打印时自增。

public class YourService extends Service {
public int counter=0;

    @Override
    public void onCreate() {
        super.onCreate();
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O)
            startMyOwnForeground();
        else
            startForeground(1, new Notification());
    }

    @RequiresApi(Build.VERSION_CODES.O)
    private void startMyOwnForeground()
    {
        String NOTIFICATION_CHANNEL_ID = "example.permanence";
        String channelName = "Background Service";
        NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
        chan.setLightColor(Color.BLUE);
        chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
        
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        assert manager != null;
        manager.createNotificationChannel(chan);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
        Notification notification = notificationBuilder.setOngoing(true)
                .setContentTitle("App is running in background")
                .setPriority(NotificationManager.IMPORTANCE_MIN)
                .setCategory(Notification.CATEGORY_SERVICE)
                .build();
        startForeground(2, notification);
    }


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


    @Override
    public void onDestroy() {
        super.onDestroy();
        stoptimertask();

        Intent broadcastIntent = new Intent();
        broadcastIntent.setAction("restartservice");
        broadcastIntent.setClass(this, Restarter.class);
        this.sendBroadcast(broadcastIntent);
    }



    private Timer timer;
    private TimerTask timerTask;
    public void startTimer() {
        timer = new Timer();
        timerTask = new TimerTask() {
            public void run() {
                Log.i("Count", "=========  "+ (counter++));
            }
        };
        timer.schedule(timerTask, 1000, 1000); //
    }

    public void stoptimertask() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

创建一个广播接收器来响应您在Restarter.java中自定义的广播

您刚刚在YourService.java 中定义的动作名称为"restartservice" 的广播现在应该触发一个方法,该方法将重新启动您的服务。这是在 Android 中使用 BroadcastReceiver 完成的。

我们重写了 BroadcastReceiver 中内置的onRecieve() 方法来添加将重新启动服务的语句。 startService() 在 Android Oreo 8.1 及更高版本中将无法正常工作,因为一旦应用被终止,严格的后台政策将在重启后很快终止服务。因此,我们将startForegroundService() 用于更高版本并显示持续通知以保持服务运行。

public class Restarter extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("Broadcast Listened", "Service tried to stop");
        Toast.makeText(context, "Service restarted", Toast.LENGTH_SHORT).show();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            context.startForegroundService(new Intent(context, YourService.class));
        } else {
            context.startService(new Intent(context, YourService.class));
        }
    }
}

定义您的 MainActivity.java 以在应用启动时调用服务。

这里我们定义了一个单独的isMyServiceRunning()方法来检查后台服务的当前状态。如果服务没有在运行,我们使用startService()启动它。

由于应用程序已经在前台运行,我们不需要将服务作为前台服务启动以防止自身被终止。

请注意,在onDestroy() 中,我们专门调用stopService(),以便调用我们的重写方法。如果不这样做,那么服务将在应用被终止后自动结束,而无需在 YourService.java

中调用我们修改后的onDestroy() 方法
public class MainActivity extends AppCompatActivity {
    Intent mServiceIntent;
    private YourService mYourService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mYourService = new YourService();
        mServiceIntent = new Intent(this, mYourService.getClass());
        if (!isMyServiceRunning(mYourService.getClass())) {
            startService(mServiceIntent);
        }
    }

    private boolean isMyServiceRunning(Class<?> serviceClass) {
        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (serviceClass.getName().equals(service.service.getClassName())) {
                Log.i ("Service status", "Running");
                return true;
            }
        }
        Log.i ("Service status", "Not running");
        return false;
    }


    @Override
    protected void onDestroy() {
        //stopService(mServiceIntent);
        Intent broadcastIntent = new Intent();
        broadcastIntent.setAction("restartservice");
        broadcastIntent.setClass(this, Restarter.class);
        this.sendBroadcast(broadcastIntent);
        super.onDestroy();
    }
}

最后在你的AndroidManifest.xml注册他们

以上三个类都需要分别在AndroidManifest.xml注册。

请注意,我们将带有动作名称intent-filter定义为"restartservice",其中Restarter.java注册为receiver。 这确保了我们的自定义 BroadcastReciever 在系统遇到具有给定动作名称的广播时被调用。

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <receiver
        android:name="Restarter"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="restartservice" />
        </intent-filter>
    </receiver>

    <activity android:name="MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service
        android:name="YourService"
        android:enabled="true" >
    </service>
</application>

如果应用程序从任务管理器中终止,现在应该会再次重新启动您的服务。只要用户不在应用程序设置Force Stop应用程序,此服务就会继续在后台运行。

更新:感谢Dr.jacky 指出这一点。上述方法仅在调用服务的onDestroy() 时才有效,在某些时候可能不是,这是我不知道的。谢谢。

【讨论】:

  • 当应用程序被杀死或在奥利奥后台时,这仍然无法在后台工作。它仅在应用程序处于前台时才起作用。 ..请帮助解决这个问题。
  • 你在 MIUI 上测试它吗?如果是,我很遗憾地说,但他们有严格的后台任务政策,他们无条件地杀死所有未列入白名单的应用程序。
  • 服务的onDestroy 可能不会被调用:stackoverflow.com/a/7237522/421467
  • 谢谢!我更新了我的答案以确保知道这种可能性。我今天学到了一些新东西。
  • 当我们已经从 onStartCommand() 返回了 START_STICKY ,这意味着如果服务被杀死,操作系统将重新启动服务。那么为什么我们需要一个在服务的 onDestroy() 中启动服务的广播呢?操作系统不是已经在处理重启了吗?
【解决方案2】:

如果您的服务是由您的应用启动的,那么实际上您的服务正在主进程上运行。因此,当应用程序被杀死时,服务也将停止。因此,您可以做的是,从您的服务的onTaskRemoved 方法发送广播,如下所示:

 Intent intent = new Intent("com.android.ServiceStopped");
 sendBroadcast(intent);

并有一个广播接收器,它将再次启动服务。我试过了。服务从所有类型的杀戮中重新启动。

【讨论】:

  • 这是正确的答案。我在blog entry 中写了一个简单的例子
  • 请告诉我我在意图中写的是什么意思是“com.android.ServiceStopped”是什么意思我在 sendBroadcast 方法中调用的意图是创建一个停止服务的新意图
  • @BhendiGawaar 我和你说的一样。但它不适用于 vivo、OPO、MIUI 等定制 OS 手机。
  • 有时当 android 指定它需要杀死一些进程以释放更多内存时,它甚至不会调用 onTaskRemoved 表单服务。它只是杀死它。
  • 我使用的是联想 TB3-730X。当应用程序被用户杀死时不会调用 onTaskRemoved 并且我也尝试过使用 onDestroy 。它也不起作用。请给我建议任何解决方案
【解决方案3】:

在您的服务中,添加以下代码。

@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);
 }

【讨论】:

  • 要摆脱“缺少 PendingIntent 可变性标志”警告,请将 PendingIntent.FLAG_ONE_SHOT 替换为 PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE
【解决方案4】:

在 onstart 命令里面放 START_STICKY... 这个服务不会杀掉,除非它正在做太多的任务并且内核想要为它杀掉它...

@Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            Log.i("LocalService", "Received start id " + startId + ": " + intent);
            // We want this service to continue running until it is explicitly
            // stopped, so return sticky.
            return START_STICKY;
        }

【讨论】:

  • 适用于通过任务管理器杀死,但该服务无法在“长时间按下后退按钮”杀死:/
  • start sticky会在应用关闭后再次运行服务,所以从某种意义上说它会重启服务。
  • 在某些时候,如果用户杀死了应用程序,也许我们应该尊重用户并让服务死亡。
【解决方案5】:

原因是您正在尝试使用 IntentService。这是来自API Docs 的行

IntentService 执行以下操作:

在处理完所有启动请求后停止服务,因此您 永远不必调用 stopSelf()。

因此,如果您希望您的服务无限期运行,我建议您改为扩展 Service 类。但是,这并不能保证您的服务将无限期运行。如果优先级较低,您的服务仍有可能在内存不足的状态下被内核杀死。所以您有两种选择:
1)通过调用startForeground()方法使其在前台运行。
2)如果服务被杀死,请重新启动服务。 这是文档中示例的一部分,他们讨论了在服务被杀死后重新启动服务

 public int onStartCommand(Intent intent, int flags, int startId) {
      Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();

      // For each start request, send a message to start a job and deliver the 
      // start ID so we know which request we're stopping when we finish the job 
      Message msg = mServiceHandler.obtainMessage();
      msg.arg1 = startId;
      mServiceHandler.sendMessage(msg);

      // If we get killed, after returning from here, restart 
      return START_STICKY;
  }  

【讨论】:

    【解决方案6】:

    您可以在清单中使用android:stopWithTask="false",如下所示,这意味着即使用户通过从任务列表中删除应用程序来杀死应用程序,您的服务也不会停止。

     <service android:name=".service.StickyService"
                      android:stopWithTask="false"/>
    

    【讨论】:

    • 默认为假!!
    【解决方案7】:

    在你的服务类中使用 onTaskRemoved

     @Override
        public void onTaskRemoved(Intent rootIntent) {
            Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());
    
            PendingIntent restartServicePendingIntent = PendingIntent.getService(
                    getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);
            AlarmManager alarmService = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            alarmService.set(AlarmManager.ELAPSED_REALTIME, elapsedRealtime() + 500,
                    restartServicePendingIntent);
            Log.d("taskremoved", "task removed ");
            super.onTaskRemoved(rootIntent);
        }
    

    【讨论】:

      【解决方案8】:

      你试试下面的代码:

      public class HeartbeartService extends Service {
      
      private static final int SERVICE_NOTIFICATION_ID = 54321;
      private static final String CHANNEL_ID = "Notification service";
      private final LocalBinder mBinder = new LocalBinder();
      
      Handler handler = new Handler();
      private Runnable runnableCode = new Runnable() {
          @Override
          public void run() {
              Context context = getApplicationContext();
      
              Intent myIntent = new Intent(context, HeartbeatEventService.class);
              context.startService(myIntent);
              HeadlessJsTaskService.acquireWakeLockNow(context);
              handler.postDelayed(this, 2000);
              
          }
      };
      
      public class LocalBinder extends Binder {
          public HeartbeartService getService() {
              return HeartbeartService.this;
          }
      }
      
      private void createNotificationChannel() {
          // Create the NotificationChannel, but only on API 26+ because
          // the NotificationChannel class is new and not in the support library
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
              int importance = NotificationManager.IMPORTANCE_MIN;
              NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "Notification service", importance);
              channel.setDescription("CHANEL DESCRIPTION");
              NotificationManager notificationManager = getSystemService(NotificationManager.class);
              notificationManager.createNotificationChannel(channel);
          }
      }
      
      
      
      
      
      @Override
      public void onCreate() {
          super.onCreate();
         
      }
      
      @Override
      public void onDestroy() {
          super.onDestroy();
          this.handler.removeCallbacks(this.runnableCode);
         
      }
      
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
          this.handler.post(this.runnableCode);
           createNotificationChannel();
           Intent notificationIntent = new Intent(this, MainActivity.class);
           PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
                   PendingIntent.FLAG_CANCEL_CURRENT);
           Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                   .setContentTitle("Notification service")
                   .setContentText("Running...")
                   .setBadgeIconType(0)
                   .setAutoCancel(false)
                   .setOngoing(true)
                   .build();
      
          startForeground(1, notification);
        
          return START_STICKY;
      }
      

      }

      【讨论】:

      • 请补充说明。
      • 你需要解释什么?我希望可以帮助你。阿杰·乔汉
      猜你喜欢
      • 1970-01-01
      • 2018-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-08
      • 2013-11-02
      相关资源
      最近更新 更多