gpt4 book ai didi

Android:服务被杀死并在一段时间后重新启动

转载 作者:行者123 更新时间:2023-11-29 02:41:48 24 4
gpt4 key购买 nike

我知道这是一个众所周知的主题,但我已经尝试了很多东西。我有一个简单的应用程序,专用于特定用户,应用程序有一个 mainActivity,它在屏幕上显示一些状态,它正在启动两项服务,一项是从服务器发出请求(每 5 分钟一次),一项是发送短信和重播到服务器(每十分钟)。

该应用程序运行在装有 Android 4.4.2 的 Samsung pocket 2 上,此设备仅用于此应用程序。当设备连接到 ADB 时,服务工作正常,但如果我断开电话并让它正常运行,服务将重复终止并在一段时间后重新启动。消息的发送非常延迟。如果有任何建议,我将不胜感激。

这是我的代码:

主要 Activity :

public class MainActivity extends Activity {

private TextView _internet;
private TextView _signal;
private TextView _server;
private BroadcastReceiver receiver;

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

IntentFilter intentFilter = new IntentFilter(Constants.SS);

receiverWorker();
registerReceiver(receiver, intentFilter);

startService(new Intent(this, RefreshDBService.class));
startService(new Intent(this, SmsService.class));


}

private void receiverWorker() {
receiver = new BroadcastReceiver() {

public void onReceive(Context arg0, Intent arg1) {
checkState();
}};

}

public void refreshButonClicked(View v) {
checkState();
}`

这是我的第一个服务:

public class RefreshDBService extends Service {

private Thread _backgroundWork;
private ScheduledExecutorService scheduleTaskExecutor = Executors.newScheduledThreadPool(1);
private DataBaseOperations _dataSource;

@Override
public void onCreate() {
super.onCreate();

_dataSource = new DataBaseOperations(this);

_backgroundWork = new Thread(new Runnable() {
@Override
public void run() {

if(Checks.checkInternetConnection(getApplicationContext())){
if(ServerOperations.isServerAvailable(getApplicationContext())){

String inputData = ServerOperations.makeRequest(Constants.GET_DATA_ROUTE, ServerOperations.getMessagesFromServer(getApplicationContext()));

ArrayList<DataSmsObj> dataFromServer=null;
if(inputData!=null && !inputData.isEmpty()){
dataFromServer = ServerOperations.fromJsonToObjects(inputData);

if(dataFromServer.size()>0){
_dataSource.open();
_dataSource.insertDataFromServer(dataFromServer);
_dataSource.close();
}
}
System.out.println("check server for messages in pending status, received -> "+ dataFromServer.size());

}else{
System.out.println("no server");
sentErrorToUI(Constants.NO_SERVER);
}
}else{
System.out.println("no internet");
sentErrorToUI(Constants.NO_INTERNET);
}


}
});
}


public int onStartCommand(Intent intent, int flags, int startId) {
scheduleTaskExecutor.scheduleWithFixedDelay(_backgroundWork, 0, Constants.NEXT_CYCLE/2, TimeUnit.MINUTES);
return START_REDELIVER_INTENT;
}

@Override
public void onDestroy() {
super.onDestroy();
scheduleTaskExecutor.shutdownNow();
}

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

private void sentErrorToUI(String message){
Intent intent = new Intent(Constants.SS);
intent.putExtra(Constants.SS, message);
System.out.println("trimit" +message);
sendBroadcast(intent);
}

这是第二个:

public class SmsService extends Service {

private Thread _backgroundWork;
private ScheduledExecutorService scheduleTaskExecutor = Executors.newScheduledThreadPool(1);

private DataBaseOperations _dataSource;

@Override
public void onCreate() {
super.onCreate();
_dataSource = new DataBaseOperations(this);

_backgroundWork = new Thread(new Runnable() {
@Override
public void run() {

sendFeedbackToServer();

List<DataSmsObj> dataToSent = new ArrayList<DataSmsObj>();
_dataSource.open();
dataToSent = _dataSource.getDataToSent();
_dataSource.close();

System.out.println("messages to sent: "+ dataToSent.size());
for (int i = 0; i < dataToSent.size(); i++) {

//here the messages are send, the code is to long to put it here, but if is need i can do it afterwards

}
}
});
}

public int onStartCommand(Intent intent, int flags, int startId) {
scheduleTaskExecutor.scheduleWithFixedDelay(_backgroundWork, 0, Constants.NEXT_CYCLE, TimeUnit.MINUTES);
return START_REDELIVER_INTENT;
}

@Override
public void onDestroy() {
super.onDestroy();
scheduleTaskExecutor.shutdownNow();

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

最佳答案

如果您将后台Service 与计划任务一起使用,它可能会被系统终止。防止杀戮的唯一方法是前台 Service。引用文档:

A foreground service is a service that the user is actively aware of and is not a candidate for the system to kill when low on memory.

您必须使用 Notification 在您的 Service 中调用方法 startForeground() 来显示它。有关更多信息,您可以查看:https://developer.android.com/guide/components/services.html#Foreground

顺便提一下,我建议您使用 api 21 以上的新 JobScheduler api。 https://developer.android.com/reference/android/app/job/JobScheduler.html

关于Android:服务被杀死并在一段时间后重新启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43557188/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com