Click here to Skip to main content
15,790,242 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;
    }


Java
@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;
    }


Java
@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
Java
Intent serviceIntent = new Intent(this, MyService.class);
        startService(serviceIntent);


and this is a service in androidmanifest.xml
Java
<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
Posted
Updated 9-Feb-21 22:05pm
v3
Comments
David Crow 10-Feb-21 8:30am    
"...but when i close the app, the service has been stopped"

How are you verifying this? Is the service's OnDestroy() method being called after the app is closed? What long-running code is in the service that is meant to keep it alive?

What version of Android is this for? Changes were made to v8 and newer in regard to background processes/services.

Shouldn't onStartCommand() be returning START_STICKY?
Komang Putra 10-Feb-21 8:35am    
In the second of service, i see a data keeps looping when app is active. But when i swipe close app, the data doesnt keep looping anymore..

So, is that possible to make service keeps running when app is killed ?
David Crow 10-Feb-21 8:40am    
"In the second of service..."

What exactly does this mean?
Komang Putra 10-Feb-21 8:43am    
This sir
@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;
}
Komang Putra 10-Feb-21 8:44am    
"Shouldn't onStartCommand() be returning START_STICKY?"

Yes, i already tried this but the service doesn't running when app is killed

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900