I want to create a service that is running even when app is killed so I created a unbound service so that it is not bind to activity lifecycle. But everytime I kill the app by long press and swipe, the service also get killed. Can some please help me. Thanks
Service
public class MyService extends Service {
public GCMNotificationIntentService() {
}
@Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "Service binded");
return null;
}
@Override
public boolean onUnbind(Intent intent) {
Log.d(TAG, "Service unbound");
return super.onUnbind(intent);
}
@Override
public void onCreate() {
Log.d(TAG, "Service created");
super.onCreate();
}
@Override
public void onDestroy() {
Log.d(TAG, "Service destoryed");
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "Service started");
Thread readerThread = new Thread(new Runnable() {
@Override
public void run() {
while(true) {
Log.d(TAG, "Thread present");
try {
// thread to sleep for 1000 milliseconds
Thread.sleep(3000);
} catch (Exception e) {
System.out.println(e);
}
}
}
});
readerThread.start();
return super.onStartCommand(intent, flags, startId);
}
}
and I am calling it from activity like this
startService(new Intent(MyService.class.getName()));
The service runs fine until app is in background or foreground but when I long press and sweep the app, service also stops with crash message like this
Scheduling restart of crashed service com.net.gs.MyService in 1000ms
I/ActivityManager( 1160): Force stopping service ServiceRecord{44989bf0 u0 com.net.gs.MyService}