华为的前台服务被杀(GRA-UL00)-启用受保护的应用程序 [英] Foreground Service killed on Huawei (GRA-UL00) - Protected Apps Enabled

查看:329
本文介绍了华为的前台服务被杀(GRA-UL00)-启用受保护的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个小时后,我的前台粘性服务被杀死,而没有重新启动。我知道有人问过几次了,我已经阅读并验证了设备上的所有支票。重要的是要注意,这似乎仅在华为设备上发生。

My foreground sticky service is killed after a few hours without being restarted. I know this has been asked a couple of times, and I have read and verified all the checks on my device. Its important to note that this seems to occur only on Huawei devices.

因此,请允许我提供以下详细信息。

So allow me to provide the following details.

定期服务

public class PeriodicService extends Service {
      @Override
      public void onCreate() {
           super.onCreate();
           acquireWakeLock();
           foregroundify(); 
      }

      private void foregroundify() {
            // Omitted for brevity. Yes it does starts a foreground service with a notification
            // verified with adb shell dumpsys activity processes > tmp.txt
            // entry in tmp.txt => "Proc # 1: prcp  T/S/SF trm: 0 14790:my.app.package.indentifier/u0a172 (fg-service)" 
      }

      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
           acquireWakeLock();
           if (!isServiceRunningInForeground(this, this.getClass())){
              foregroundify(); 
           }

           PeriodicAlarmManager alarmManager = PeriodicAlarmManager.get(this);
           alarmManager.setAlarm();
           return START_STICKY;  // after a few hours, service terminates after this returns. verified in my local logs 
       }

       @Override
       public IBinder onBind(Intent intent) {
          return null;
       }

       @Override
       public void onDestroy() {
            releaseWakeLock(); 
            stopForeground(true);
            super.onDestroy();
       }

    }

PeriodicAlarmManager >

public void setAlarm() {
        Intent intent = new Intent(mContext, PeriodicAlarmReceiver.class);
        intent.setAction("repeat");
        mAlarmIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
        mAlarmManager.cancel(mAlarmIntent);
        long triggerAtMillis = System.currentTimeMillis() + ALARM_INTERVAL_MINUTES;

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
            mAlarmManager.setExact(AlarmManager.RTC_WAKEUP, triggerAtMillis, mAlarmIntent);
        } else {
            mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerAtMillis, mAlarmIntent);
        }

        ComponentName receiver = new ComponentName(mContext, PeriodicBootReceiver.class);
        PackageManager pm = mContext.getPackageManager();

        pm.setComponentEnabledSetting(receiver,
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);
    }

PeriodicAlarmReceiver

public class PeriodicAlarmReceiver extends WakefulBroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent service = new Intent(context, PeriodicService.class);
        service.putExtra("source", "PeriodicAlarmReceiver");
        intent.getAction()));
        startWakefulService(context, service);
    }
}

应用

    public class MyApp extends Application {

        @Override
        public void onCreate() {
            super.onCreate();
        }

        @Override
        public void onLowMemory(){
            super.onLowMemory(); // never gets called
        }

        @Override
        public void onTrimMemory(int level){
            super.onTrimMemory(level); // only gets called on app launch
        }

        @Override
        public void onTerminate() {
            super.onTerminate();
        }
}

adb shell dumpsys活动进程> tmp。 txt

tmp.txt中的条目=>过程1:prcp T / S / SF trm:0 14790:my.app.package.indentifier / u0a172(fg-service)

Entry in tmp.txt => "Proc # 1: prcp T/S/SF trm: 0 14790:my.app.package.indentifier/u0a172 (fg-service)"

以上输入基于此处接受的答案:前景服务被Android杀死

Above Entry is based on accepted answer here: Foreground service being killed by Android

将MyApp添加到设置->高级设置中的受保护应用列表中- >电池管理器->受保护的应用程序(关闭屏幕后允许应用程序继续运行)

Added MyApp to protected app list in Settings-> Advanced Settings -> Battery Manager -> Protected Apps (Allow app to keep running after screen is turned off)

设置->高级设置->电源计划中的使用的性能(最低设置) (性能)

Used Performance (lowest setting) in Settings-> Advanced Settings -> Power Plan (Performance)

设备信息

型号:HUAWEI GRA-UL00

Model Number: HUAWEI GRA-UL00

EMUI版本:EMUI 4.0.1

EMUI Version: EMUI 4.0.1

Android版本:6.0

Android Version: 6.0

其他说明:

低记y,在终止之前不会调用onTrimMemory。无论如何,我只是为了使应用程序在后台保持活动状态而将应用程序剥离到最低限度,所以这里的内存应该不是问题。

Low Memory, onTrimMemory is not called prior to termination. In any case, I stripped the app to its bare minimum just to keep the app alive in the background, so memory should not be an issue here.

除非用户明确重新启动应用程序,否则Sticky Service永远不会重新启动。

Sticky Service is never restarted unless user explicitly re-launches the app.

未调用警报管理器来重新启动/重新创建服务。 setExactAndAllowWhileIdle()也不起作用,并且应该无关紧要,因为该服务是前台优先服务,因此不应受到打ze模式的影响。

Alarm Manager is not called to restart/recreate service. setExactAndAllowWhileIdle() does not work either, and should be irrelevant since the service is a foreground priority service, and hence should not be affected by doze mode.

服务在终止前最多只能运行12个小时。发生这种情况时,电池电量超过65%。

Service can only run at the maximum of 12 hours before being terminated. Battery was above 65% when this happened.

由于此应用程序是用于研究项目的,因此必须无限期地保持服务运行。

It is a requirement to keep the service running indefinitely as this app is for a research project.

我还能做其他事情吗?或者这是华为Android的特定修改版,开发人员对此无能为力。重申一下,此问题仅在华为设备上发生。

Is there anything else I can do or is this a specific Huawei Android modification that the developer can do nothing about. To reiterate, this issue only happens on Huawei devices.

对此有更多见解!

推荐答案

这听起来像是您的应用被华为PowerGenie杀死,因为它无限期地持有唤醒锁。如果您无法避免使用唤醒锁,请参阅我对类似问题的回答以获取解决方法。

It sounds like your app is being killed by Huawei PowerGenie because it holds a wake lock indefinitely. If you can't avoid using a wake lock, please see my answer to a similar question for a workaround.

这篇关于华为的前台服务被杀(GRA-UL00)-启用受保护的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆