gpt4 book ai didi

android - 当应用程序关闭/销毁或刷出时执行任务

转载 作者:搜寻专家 更新时间:2023-11-01 09:32:25 24 4
gpt4 key购买 nike

我想知道我是否可以在应用程序被刷出或关闭的情况下保持应用程序服务运行。我想一直将 gps 数据发送到服务器,无论应用程序是打开的还是在后台还是关闭的(从最近的应用程序列表中刷出等)。我试过“服务”但没有用。我该怎么做。某人可以给我一些提示吗?谢谢。

public class MainActivity extends AppCompatActivity{

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

startService(new Intent(getBaseContext(),TestService.class));
}

}

public class TestService extends Service {

@Override
public void onDestroy() {
super.onDestroy();
Log.e("service", " destroyed");
Toast.makeText(this, "Service destroyed", Toast.LENGTH_SHORT).show();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "service started", Toast.LENGTH_SHORT).show();
Log.e("service", " onStartCommand");
return START_STICKY;
}


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

最佳答案

你可以使用

  1. JobScheduler

This API allows you to run scheduled service and the android system will batch all the services from different applications and run them together in some particular timeframe

  1. Firebase Job Dispatcher

The Firebase JobDispatcher is a library for scheduling background jobs in your Android app. It provides a JobScheduler-compatible API that works on all recent versions of Android (API level 9+) that have Google Play services installed.

您可以使用以下链接作为引用

reference 1
reference 2
reference 3
referennce 4
reference 5
reference 6

关于android - 当应用程序关闭/销毁或刷出时执行任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45994279/

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