一旦应用程序被销毁,粘性服务就会停止

时间:2021-04-16 15:40:32

标签: java android service behavior

我有一个粘性服务,它启动一个简单的计数器并记录计数,一旦应用程序被破坏(通过最近的应用程序向上/向下滑动),服务就会停止,然后系统会在服务上调用以下方法: onCreate,onDestroy

为什么不调用 onStart ?文档指出会有一个 NULL 意图的 onStart。

代码如下:

主要活动:

package com.eddieharari.servicetest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

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

        Intent i = new Intent(MainActivity.this, Counter.class);
        startService(i);
    }
    @Override
    protected void onStop(){
        super.onStop();
        Log.d("ServiceTest","onStop called");

    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d("ServiceTest","onPause called");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("ServiceTest","onDestroy called");
    }
}

柜台服务:

package com.eddieharari.servicetest;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.MainThread;

public class Counter extends Service {
    int counter;
    public Counter() {
        counter = 0 ;
    }
    @Override
    public void onCreate() {
        super.onCreate();
        Log.d("ServiceTest","Service had been created");
      
    }
    @Override
    public int onStartCommand(Intent myInt, int serviceFlags, int startId) {
        Log.d("ServiceTest","Counter Service OnStart was called");
        mainThread myMainThread = new mainThread();
        myMainThread.start();
        return START_STICKY;
    }
    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onDestroy() {
        Log.d("ServiceTest","Counter Service got destroyed");
    }
}

主线程:

package com.eddieharari.servicetest;

import android.util.Log;

public class mainThread extends Thread{
    int counter;
    public mainThread() {
        counter = 0;
    }
    @Override
    public void run() {
        while(true) {
            counter++;
            Log.d("ServiceTest","Counter is now:"+counter);
            try {
                sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

最后 - 应用程序的输出:

2021-04-16 18:36:20.362 24030-24030/.counterProcess D/ServiceTest: Service had been created
2021-04-16 18:36:20.363 24030-24030/.counterProcess D/ServiceTest: Counter Service OnStart was called
2021-04-16 18:36:20.380 24030-24062/.counterProcess D/ServiceTest: Counter is now:1
2021-04-16 18:36:21.497 24030-24062/.counterProcess D/ServiceTest: Counter is now:2
2021-04-16 18:36:28.002 24030-24062/.counterProcess D/ServiceTest: Counter is now:3
2021-04-16 18:36:28.543 24030-24062/.counterProcess D/ServiceTest: Counter is now:4
2021-04-16 18:36:29.900 24030-24062/.counterProcess D/ServiceTest: Counter is now:5
2021-04-16 18:36:31.330 24030-24062/.counterProcess D/ServiceTest: Counter is now:6
2021-04-16 18:36:35.001 24030-24062/.counterProcess D/ServiceTest: Counter is now:7
2021-04-16 18:36:40.438 24030-24062/.counterProcess D/ServiceTest: Counter is now:8
2021-04-16 18:36:40.953 24030-24062/.counterProcess D/ServiceTest: Counter is now:9
2021-04-16 18:36:41.489 24030-24062/.counterProcess D/ServiceTest: Counter is now:10
2021-04-16 18:36:41.990 24030-24062/.counterProcess D/ServiceTest: Counter is now:11
2021-04-16 18:36:42.517 24030-24062/.counterProcess D/ServiceTest: Counter is now:12
2021-04-16 18:36:43.060 24030-24062/.counterProcess D/ServiceTest: Counter is now:13
2021-04-16 18:36:43.445 23997-23997/com.eddieharari.servicetest D/ServiceTest: onPause called
2021-04-16 18:36:43.600 24030-24062/.counterProcess D/ServiceTest: Counter is now:14
2021-04-16 18:36:43.992 23997-23997/com.eddieharari.servicetest D/ServiceTest: onStop called
2021-04-16 18:36:44.138 24030-24062/.counterProcess D/ServiceTest: Counter is now:15
2021-04-16 18:36:44.679 24030-24062/.counterProcess D/ServiceTest: Counter is now:16
2021-04-16 18:36:45.221 24030-24062/.counterProcess D/ServiceTest: Counter is now:17
2021-04-16 18:36:45.237 23997-23997/com.eddieharari.servicetest D/ServiceTest: onDestroy called
2021-04-16 18:36:46.782 24099-24099/.counterProcess D/ServiceTest: Service had been created
2021-04-16 18:36:46.783 24099-24099/.counterProcess D/ServiceTest: Counter Service got destroyed
  1. 为什么系统会在服务上调用 onCreate 和 onDestroy
  2. 一旦应用程序被销毁,我如何让服务运行...

PS - 我知道有前台服务+通知,我正在询问这种特定行为并想了解它为什么会这样......

谢谢,

埃迪。

0 个答案:

没有答案
相关问题