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 –