Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android is killing my foreground service after Force stop my package

I've been struggling with this for 2 weeks now.

I'm developing an app that controls call duration.

I receive a braodcast where I start a foreground service to hang up the call.

but after five minutes or more android force stop my package then kills my process which cause the service to crash -I think - (don't know why) with no crash message or any kind of error.

it just disappear.

And it Schedules restart but it never start the service again.

here is the logcat

 12-29 00:28:52.857 619-619/? I/ActivityManager: Force stopping package club.androidy.callcontrolfree appid=10006 user=0
 12-29 00:28:52.858 619-619/? I/ActivityManager: Killing proc 433:club.androidy.callcontrolfree/u0a10006: force stop club.androidy.callcontrolfree
 12-29 00:28:52.894 619-619/? W/ActivityManager: Scheduling restart of crashed service club.androidy.callcontrolfree/.PhoneCallService in 5000ms
 12-29 00:28:52.919 619-619/? I/ActivityManager:   Force stopping service ServiceRecord{422b1b18 u0 club.androidy.callcontrolfree/.PhoneCallService}

using adb shell dumpsys I got this which insures that my service is a foreground service with the right priority

*APP* UID 10088 ProcessRecord{41aefb98 27922:club.androidy.callcontrolfree/u0a10088}

user #0 uid=10088

class=club.androidy.callcontrolfree.AnalyticsApplication

dir=/data/app/club.androidy.callcontrolfree-1.apk publicDir=/data/app/club.androidy.callcontrolfree-1.apk data=/data/data/club.androidy.callcontrolfree

packageList=[club.androidy.callcontrolfree]

compat={240dpi}

thread=android.app.ApplicationThreadProxy@41cef800

pid=27922 starting=false lastPss=0

lastActivityTime=-11s768ms lruWeight=14097791 serviceb=false keeping=true hidden=false empty=true

oom: max=15 hidden=9 client=9 empty=15 curRaw=2 setRaw=2 nonStopping=2 cur=2 set=2

curSchedGroup=-1 setSchedGroup=-1 systemNoUi=false trimMemoryLevel=0

adjSeq=63108 lruSeq=10968

setIsForeground=false foregroundServices=true forcingToForeground=null

lastRequestedGc=-12s74ms lastLowMemory=-12s74ms reportLowMemory=false

Services:

  - ServiceRecord{41fb2578 u0 club.androidy.callcontrolfree/.PhoneCallService}

and in Process LRU list (sorted by oom_adj): section

Proc #24: adj=prcp /FS trm= 0 27922:club.androidy.callcontrolfree/u0a10088 (fg-service)

I'm not binding my service to any activities

I start my service like this:

            Intent srvIntent = new Intent(context, PhoneCallService.class);
            context.stopService(srvIntent);
            PhoneCallService.stopService = false;
            context.startService(srvIntent);

and this is my onStartCommand :

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Context context = getApplicationContext();
    String txtNotificationTicker = context.getString(R.string.strNotificationTicker);
    String txtNotificationTitle = context.getString(R.string.strNotificationContexTitle);
    String txtNotificationText = context.getString(R.string.strNotificationContexText);
    String ns = Context.NOTIFICATION_SERVICE;

    Intent cancelIntent = new Intent(this, StopServiceReceiver.class);
    PendingIntent pendingIntentCancel = PendingIntent.getBroadcast(this, 0, cancelIntent
            , PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Action action =
            new NotificationCompat.Action
                    .Builder(R.drawable.ic_cancel
                    , context.getString(R.string.stop_service), pendingIntentCancel)
                    .build();

    Intent mIntent = new Intent(this,MainActivity.class);
    int HELLO_ID = 1;
    PendingIntent pendingIntent = PendingIntent.getActivity(this, HELLO_ID, mIntent , 0);
    this.mNotificationManager = (NotificationManager) getSystemService(ns);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

    Notification notification = builder
            .setContentIntent(pendingIntent)
            .setContentTitle(new StringBuilder(String.valueOf(txtNotificationTitle)))
            .setSmallIcon(R.drawable.ic_launcher)
            .setLargeIcon(getBitmap(context, R.drawable.ic_launcher))
            .setContentText(new StringBuilder(String.valueOf(txtNotificationText))
                    .append(": ").append(PHONE_NUMBER).toString())
            .setWhen(System.currentTimeMillis())
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .addAction(action)
            .build();

    notification.flags |= Notification.FLAG_NO_CLEAR;
    startForeground(1, notification);
    this.pt = new PhoneThread();
    this.pt.start();
    return Service.START_STICKY;
}

I'm acquiring wake lock like this

        if (PhoneCallService.sCpuWakeLock == null) {
                PhoneCallService.sCpuWakeLock = ((PowerManager)
                        context.getSystemService(Context.POWER_SERVICE))
                        .newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"CCF");
                PhoneCallService.sCpuWakeLock.acquire();
            Log.e("androidy","wacklock acquired");
            }

I have already tried many solutions from SO and google issues but nothing works for me.

I saw and tried all these :

Foreground service killed by OS

Foreground service gets killed every time

Android foreground service being killed under certain conditions

Foreground Service being killed on Notification click

Foreground service being killed by Android

Users who keep the app are only 30% from total installs.

What I'm doing wrong?

like image 664
Mohammad Avatar asked Oct 19 '22 19:10

Mohammad


1 Answers

Edit:

If you have the same issue please check Power Saving Options in your phone settings if your phone has this feature it will kill your app after the screen is off and will not allow it to work in background.

Original answer: After a lot of search I solved it myself

it turned out that my app was power-intensive as shown in the picture below

enter image description here

Hence, android was force stopping it after 5 minutes when there is no activity on the screen to save power.

I solved power consumption issues and everything became OK.

enter image description here

like image 174
Mohammad Avatar answered Oct 21 '22 11:10

Mohammad