Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Foreground service gets killed on performing internet related operations

UPDATE: Previously I couldn't find a well defined pattern as to when my foreground service was being killed. After more debugging with the devices (doesn't happen on all) on which this was happening I found.

1.) A lot of times when I open chrome to load a website the foreground service gets killed. Sometimes even when I am using whatsapp this happens.

2.) There are no exceptions and the stacktrace doesn't show anything useful.

Original Question below:

There are many such questions on StackOverflow but the answers so far that I have read mostly say that it is upto Android and we don't have 100% guarantee that a foreground service will not be killed. Some answers suggest START_STICKY but that is not much helpful in my case.

In my case I have a music player app which has a foreground service. This service gets killed on certain devices, mostly some versions of Xiomi (Android version was 5.1.1). Now I understand that android might be short on memory and so my foreground service is being killed, but then why do other music player apps never go through such termination. What is it that they are doing right that I am not?

I made my service foreground service by using startForeground. Also I return START_STICKY in onStartCommand although that doesn't help because the service is restarted after a period of 4-5 sec if killed. To bind my service with my activity I use

bindService(playIntent, musicConnection, Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT );

So what exactly can I improve/change in my app to prevent this from happening, if other apps are working right there must be something that is wrong in my case. Can someone please help. Thanks in advance !!

Edit:

This is how I call startForeground()

public void sendNotification() {

        Intent notIntent = new Intent(this, MainActivity.class);
        notIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendInt = PendingIntent.getActivity(this, 0,
                notIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        Bitmap bitmap = null;
        if (!notificationShowing || !forwarded) {
            Log.i(TAG, "present");
            String title = CommonUtils.getSongFromID(songIndex, this);

            bigView.setTextViewText(R.id.title, title);
            bigView.setImageViewBitmap(R.id.img, bitmap);

            smallView.setTextViewText(R.id.title1, title);
            smallView.setImageViewBitmap(R.id.img1, bitmap);

            if (pauseButton == 1) {
                bigView.setImageViewResource(R.id.pause, R.drawable.pause_noti);
                smallView.setImageViewResource(R.id.pause1, R.drawable.pause_noti);
            } else {
                bigView.setImageViewResource(R.id.pause, R.drawable.play_noti);
                smallView.setImageViewResource(R.id.pause1, R.drawable.play_noti);
            }

            musicNotification = builder.setContentIntent(pendInt)
                    .setSmallIcon(R.drawable.logo1)
                    .setTicker(songTitle)
                    .setOngoing(true)
                    .setContentTitle("Playing")
                    .setStyle(new Notification.BigTextStyle().bigText("Song App"))
                    .setContentText(songTitle)
                    .setPriority(Notification.PRIORITY_MAX)
                    .build();

            musicNotification.contentView = smallView;
            musicNotification.bigContentView = bigView;

            musicNotification.contentIntent = pendInt;

            Intent switchIntent = new Intent("pause");
            switchIntent.putExtra("button", "pause");
            PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 100, switchIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            bigView.setOnClickPendingIntent(R.id.pause, pendingSwitchIntent);
            smallView.setOnClickPendingIntent(R.id.pause1, pendingSwitchIntent);

            Intent switchIntent1 = new Intent("forward");
            switchIntent1.putExtra("button", "forward");
            PendingIntent pendingSwitchIntent2 = PendingIntent.getBroadcast(this, 100, switchIntent1, PendingIntent.FLAG_UPDATE_CURRENT);
            bigView.setOnClickPendingIntent(R.id.forward, pendingSwitchIntent2);
            smallView.setOnClickPendingIntent(R.id.forward1, pendingSwitchIntent2);

            Intent switchIntent2 = new Intent("previous");
            switchIntent2.putExtra("button", "previous");
            PendingIntent pendingSwitchIntent3 = PendingIntent.getBroadcast(this, 100, switchIntent2, PendingIntent.FLAG_UPDATE_CURRENT);
            bigView.setOnClickPendingIntent(R.id.previous, pendingSwitchIntent3);
            smallView.setOnClickPendingIntent(R.id.previous1, pendingSwitchIntent3);

            Intent switchIntent3 = new Intent("end");
            switchIntent3.putExtra("button", "end");
            PendingIntent pendingSwitchIntent4 = PendingIntent.getBroadcast(this, 100, switchIntent3, PendingIntent.FLAG_UPDATE_CURRENT);
            bigView.setOnClickPendingIntent(R.id.end, pendingSwitchIntent4);
            smallView.setOnClickPendingIntent(R.id.end1, pendingSwitchIntent4);

            startForeground(NOTIFY_ID, musicNotification);
            notificationShowing = true;
        }
        forwarded = false;

    }
like image 614
varunkr Avatar asked Sep 10 '16 16:09

varunkr


People also ask

Can foreground service be killed?

What sets it apart from the regular Service is that it will continue to run even when the user is not interacting with the app. The Android OS tries by all means not to 'kill' a foreground Service save for extreme circumstances.

What is a foreground service?

Foreground services show a status bar notification, so that users are actively aware that your app is performing a task in the foreground and is consuming system resources. Devices that run Android 12 (API level 31) or higher provide a streamlined experience for short-running foreground services.

What is the difference between foreground and background services on Android?

A Background Service is a service that runs only when the app is running so it'll get terminated when the app is terminated. A Foreground Service is a service that stays alive even when the app is terminated.

How do you stop a foreground service?

Inside the onReceive() method I call stopforeground(true) and it hides the notification. And then stopself() to stop the service.


1 Answers

This happened in Xiomi phone due to below reason.

Solution for MIUI 7.0 => Security => Autostart => select Apps that you want to run in background => Reboot After reboot your device should able to run your application services in background like other android devices do.

MIUI 4.0 settings

MIUI AutoStart Detailed Description

And if you looking for other phone then check here is service structure.It automatically restart but when you restart phone call BootReceiver.

public class AppService extends Service {

private class LocalBinder extends Binder {
    public AppService getServerInstance() {

        return AppService.this;
    }
}


@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // If we get killed, after returning from here, restart

    return Service.START_STICKY;
}

@Override
public void onCreate() {
    super.onCreate();

} 

@Override
public void onDestroy() {

}

}

Thanks hope this will help you.

like image 186
Saveen Avatar answered Sep 22 '22 06:09

Saveen