小编h.h*_*abi的帖子

Forgeround 服务在几分钟后停止或锁定电话

我尝试创建每 20 秒发送一条消息的前台服务,但此服务在几分钟后停止或锁定电话。我在 android 8 (O) 中测试了这个服务。我的代码是:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
  input = intent.getStringExtra("inputExtra");

  new Thread(new Runnable() {
    public void run() {
      while (progressStatus < 10000000) {
        progressStatus += 1;           
        handler.post(new Runnable() {
          @SuppressLint("MissingPermission")
          public void run() {
            Intent notificationIntent = new Intent(ExampleService.this, MainActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(ExampleService.this,
              0, notificationIntent, 0);
            String mmm = CHANNEL_ID;

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
              notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
              id = "id_product";
              // The user-visible name of the channel. …
Run Code Online (Sandbox Code Playgroud)

android background-foreground foregroundnotification foreground-service

5
推荐指数
1
解决办法
2388
查看次数