5

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

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

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

但是在五分钟或更长时间后,android force stop 我的包然后杀死了我的进程,导致服务崩溃 - 我认为 - (不知道为什么)没有崩溃消息或任何类型的错误。

它就消失了。

它计划重新启动,但它永远不会再次启动服务。

这是日志猫

 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 dumpsysI got this 确保我的服务是具有正确优先级的前台服务

*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 问题的解决方案,但对我来说没有任何效果。

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

被操作系统杀死的前台服务

前台服务每次都被杀死

Android前台服务在某些情况下被杀死

前台服务在通知单击时被杀死

前台服务被Android杀死

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

我做错了什么?

4

1 回答 1

3

编辑:

如果您有同样的问题,请检查手机设置中的省电选项,如果您的手机具有此功能,它将在屏幕关闭后杀死您的应用程序,并且不允许它在后台运行。

原始答案: 经过大量搜索,我自己解决了

事实证明我的应用程序很耗电,如下图所示

在此处输入图像描述

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

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

在此处输入图像描述

于 2016-01-15T18:32:30.037 回答