I wonder if I can keep the app service running inspite of the app is swiped out or closed. I want to send the gps data to the server all the time whether the app is opened or in the background or is closed (swiped out from the recent app list etc). I've tried 'service' but is not working. How can I do it. Can sb give me some hints on it please? Thankyou.
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;
}
}