0
不知应用程序已被刷出或关闭,是否可以让应用程序服务继续运行。无论应用程序是打开还是后台打开,或者关闭(从最近的应用程序列表中删除),我都希望始终将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;
}
}
你可能要考虑的'JobScheduler' API:HTTPS: //developer.androi d.com/reference/android/app/job/JobScheduler.html –