强制停止我的包后,Android正在杀死我的前台服务

时间:2015-12-28 23:21:40

标签: java android service foreground-service

我现在已经挣扎了两个星期了。

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

我收到了一个braodcast,我开始使用前台服务来挂断电话。

但是五分钟或更长时间后,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和谷歌问题的许多解决方案,但对我没什么用。

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

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%。

我做错了什么?

1 个答案:

答案 0 :(得分:3)

修改

如果您遇到同样的问题,请在手机设置中查看省电选项,如果您的手机具有此功能,则会在屏幕关闭后终止您的应用,并且不允许它在后台运行

原始回答: 经过大量的搜索,我自己解决了这个问题

原来我的应用程序功耗很大,如下图所示

enter image description here

因此,当屏幕上没有任何活动以节省电量时,机器人在5分钟后强制停止它。

我解决了耗电问题,一切都变好了。

enter image description here