java - 一旦应用程序被销毁,粘性服务就会停止
问题描述
我有一个粘性服务,它启动一个简单的计数器并记录计数,一旦应用程序被销毁(通过最近的应用程序向上/向下滑动)服务停止并且系统在服务上调用以下方法: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
- 为什么系统会在服务上调用 onCreate 和 onDestroy
- 一旦应用程序被破坏,我怎样才能使服务运行......
PS - 我知道有前台服务+通知,我正在询问这种特定行为并想了解它为什么会这样......
谢谢 ,
埃迪。
解决方案
推荐阅读
- javascript - 如何使用jquery将关注按钮从关注更改为关注
- mysql - Redis和mysql的qps是多少
- git - 将一个 git 存储库复制到另一个具有历史记录的存储库
- google-cloud-platform - 我可以保护计算引擎实例只允许云运行和我的个人家庭 ip(用于测试)吗?
- php - 为什么一个子页面的全局变量在另一个子页面中不可见?
- flutter - 将数据从自定义小部件文本字段传递到计算器颤振
- flask - 在 SQLAlchemy 中,如何在冲突时重试插入唯一 ID?
- python - 我怎样才能更好地优化我的程序来划分给定的总和?(Python)
- python - 用于编写 WAV 文件元数据的 Python 库
- ios - 如何将递归数据映射到结构中?