java - 强制停止我的包后,Android 正在终止我的前台服务

标签 java android service foreground-service

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

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

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

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

它就这样消失了。

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

这是日志

 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 列表(按 oom_adj 排序): 部分

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

我不会将我的服务绑定(bind)到任何 Activity

我这样开始我的服务:

            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 issues 的解决方案,但没有一个对我有用。

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

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

我做错了什么?

最佳答案

编辑:

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

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

原来我的应用是耗电的,如下图所示

enter image description here

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

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

enter image description here

关于java - 强制停止我的包后,Android 正在终止我的前台服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34502249/

相关文章:

android - 显示服务中的相机表面

java - Spring MVC 的问题。如何从两个或多个对象创建 View ?

java - netbeans 中的组合框

java - 无法解析完成方法——从 Android Studio 方法调用

java - 属性 android :forceQueryable not found in Android studio when running Espresso test

android - 让服务知道方法何时运行

java - 关闭套接字是否关闭流?

java - 部署在 Wildfly 14 中有效,但在 15 中无效。 IllegalArgumentException 不能同时要求和提供相同的依赖项

android - 在android启动画面中为状态栏着色

c# - 从 Web 服务返回域对象