Android 在强制停止我的包裹后终止了我的前台服务

Android is killing my foreground service after Force stop my package

我已经为此苦苦挣扎了 2 周了。

我正在开发一个控制通话时长的应用程序。

我收到一个广播,我在其中启动前台服务以挂断电话。

但在五分钟或更长时间后 android 强制停止我的程序包然后杀死我的进程导致服务崩溃 - 我认为 - (不知道为什么)没有崩溃消息或任何类型的错误.

它就这么消失了。

它会安排重新启动,但它再也不会启动该服务。

这里是 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}

使用 adb shell dumpsys 我得到了这个,它确保我的服务是具有正确优先级的前台服务

*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}

并在 Process LRU list (sorted by oom_adj): 部分

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

我不会将我的服务绑定到任何活动

我这样开始我的服务:

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

这是我的 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;
}

我正在像这样获取唤醒锁

        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");
            }

我已经尝试过许多来自 SO 和 google 问题的解决方案,但对我来说没有任何效果。

我看到并尝试了所有这些:

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

保留该应用的用户仅占总安装量的 30%。

我做错了什么?

编辑:

如果您遇到同样的问题,请检查 phone 设置中的 省电选项 如果您的 phone 具有此功能,它会在之后终止您的应用程序屏幕关闭,不允许它在后台工作。

原回答: 经过大量搜索我自己解决了

原来我的应用是power-intensive如下图

因此,android在屏幕上没有activity的情况下,在5分钟后强制停止以节省电量。

我解决了功耗问题,一切正常。