Java 一旦应用程序被破坏,粘性服务就会停止
我有一个粘性服务,它启动一个简单的计数器并记录计数,一旦应用程序被破坏(通过最近的应用程序向上/向下滑动),服务停止,然后系统调用服务上的以下方法: 一次创建,一次存储 为什么不叫onStart?文档说明将有一个具有空意图的onStart 代码如下: 主要活动:Java 一旦应用程序被破坏,粘性服务就会停止,java,android,service,behavior,Java,Android,Service,Behavior,我有一个粘性服务,它启动一个简单的计数器并记录计数,一旦应用程序被破坏(通过最近的应用程序向上/向下滑动),服务停止,然后系统调用服务上的以下方法: 一次创建,一次存储 为什么不叫onStart?文档说明将有一个具有空意图的onStart 代码如下: 主要活动: package com.eddieharari.servicetest; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent;
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
Eddie.由于android 8引入了后台执行限制,粘性服务几乎不起作用。从本质上说,当您从recents刷取应用程序时,它的进程会立即停止,并且应用程序会从活动池中启动,因此不再允许它启动后台服务。不完全清楚为什么调用create,对我来说,它看起来像是一种意外行为,应用程序没有意识到它已经关闭,仍然试图重新启动一个粘性服务。