Cristian
Cristian

Reputation: 514

Why is my Android service killed by the system?

I am learning how to use Services in Android. I'm developing an app to understand them. The app will start a Service when user press a 'Start' button. The service will be running until user press a 'Stop' button (an app like Alpify or similar)

In AndroidManifest, my service is declared as follow:

<service 
    android:name="com.cpalosrejano.example.MyService"
    android:stopWithTask="false"
    android:enabled="true" />

From activity, I start the service as follow:

Intent service = new Intent(MyActivity.this, MyService.class);
startService(service);

Then I open other apps which consumes a lot of RAM, (Clash Royale, Instagram, Facebook, etc...) and my service is killed by system.

I implemented onTrimMemory(int level) method to see what is happening. Before my service is being killed, the logcat give me the follow information:

onTrimMemory() : 5 
onTrimMemory() : 10
onTrimMemory() : 15

I have read the behavior of onTrimMemory() method. The documentation says when I receive that codes, I must release unused object. But my service has no code yet.

What I tried:

The code of my service:

public class MyService extends Service {

    PowerManager.WakeLock mWakeLock;

    @Override
    public void onCreate() {
        Log.i(ServiceBase.class.getSimpleName(), "onCreate() : Service Started.");
        super.onCreate();
    }

    @Override
    public final int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(ServiceBase.class.getSimpleName(), "onStarCommand() : Received id " + startId + ": " + intent);

        // acquire wakelock
        PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
        mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, ServiceBase.class.getSimpleName());
        mWakeLock.acquire();

        // start foreground
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setSmallIcon(android.R.drawable.stat_sys_download);
        builder.setContentText("Service in foreground");
        builder.setContentTitle("My app");
        builder.setOngoing(true);
        Notification notification = builder.build();
        startForeground(1359, notification);

        // run until explicitly stopped.
        return START_STICKY;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.i(ServiceBase.class.getSimpleName(), "onBind() : true");
        return null;
    }

    @Override
    public void onRebind(Intent intent) {
        Log.i(ServiceBase.class.getSimpleName(), "onRebind() : true");
        super.onRebind(intent);
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.i(ServiceBase.class.getSimpleName(), "onUnbind() : false");
        return super.onUnbind(intent);
    }

    @Override
    public void onDestroy() {
        Log.i(ServiceBase.class.getSimpleName(), "onDestroy()");
        super.onDestroy();
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        Log.i(ServiceBase.class.getSimpleName(), "onTaskRemoved()");
        super.onTaskRemoved(rootIntent);
    }

    @Override
    public void onTrimMemory(int level) {
        Log.i(ServiceBase.class.getSimpleName(), "onTrimMemory() : " + level);
        super.onTrimMemory(level);
    }

}

And now, my question:

How is possible, Runtastic app is running while my app is being killed by system?

Upvotes: 0

Views: 953

Answers (1)

Dabar Parihar
Dabar Parihar

Reputation: 77

According to my understanding process which does not have any running component will be terminated first as compared to processes with the running component.

Android might decide to shut down a process at some point, when memory is low and required by other processes that are more immediately serving the user.

Please refer to this link https://developer.android.com/guide/components/processes-and-threads.html

Upvotes: 2

Related Questions