I need help to solve this problem, i just tried every time but my service keep killed when app is killed/swipe close..
here is my code for service
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
sharedPreferences = getSharedPreferences("user_details", MODE_PRIVATE);
refresh = new Runnable() {
public void run() {
String nik_baru = sharedPreferences.getString(KEY_NIK, null);
System.out.println("nik baru = " + nik_baru);
handler.postDelayed(refresh, 15000);
}
};
handler.post(refresh);
return Service.START_NOT_STICKY;
}
@Override
public void onTaskRemoved(Intent rootIntent) {
Intent restartService = new Intent(getApplicationContext(), this.getClass());
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartService, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME, 100, pendingIntent);
}
and this is the code i used for running service
Intent serviceIntent = new Intent(this, MyService.class);
startService(serviceIntent);
and this is a service in androidmanifest.xml
<service
android:name=".Item.MyService"
android:launchMode="singleTop"
android:enabled="true"
android:exported="true"
android:stopWithTask="false">
</service>
What I have tried:
The service keeps running when app is online. but when i close the app,
the service has been stopped