首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >adb shell顶部显示了我的应用程序在前台,但实际上它在后台。

adb shell顶部显示了我的应用程序在前台,但实际上它在后台。
EN

Stack Overflow用户
提问于 2015-09-21 17:27:18
回答 2查看 3K关注 0票数 2

我有Android应用程序,有一些绑定到某个进程的服务。

我发现adb shell top -n 1返回:

代码语言:javascript
代码运行次数:0
运行
复制
PID PR CPU% S  #THR     VSS     RSS PCY UID      Name
31647  0   1% S    70 1733640K  98960K  bg u0_a132  com.my.app.dev
31727  0   1% S    29 1523892K  62924K  fg u0_a132  com.my.app.dev:myService

即使我的应用程序处于后台,为什么top PCY 告诉 'fg' a.e。前台?

有人能在这个问题上宣传一下吗?

这是我的Manifest.xml:

代码语言:javascript
代码运行次数:0
运行
复制
 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
          package="com.my.app.dev" 
          android:versionName="4.0.1.6700000"
          android:versionCode="5033" >
    
    <uses-sdk android:minSdkVersion="10"
              android:targetSdkVersion="16" />
    
    
    
    
        
    <application android:icon="@drawable/icon" 
                 android:label="@string/config_app_name" 
                 android:theme="@style/Theme.NoActionBarAppCompat" 
                 android:name="com.my.app.Mine" > 
                 
        <!-- we would prefer the launchMode to be standard, but it causes a lot of problems. -->
        <activity android:name="com.my.ui.main.MineApp" 
                  android:label="@string/config_app_name"
                  android:launchMode="singleTask" 
                  android:clearTaskOnLaunch="true" 
                  android:theme="@style/Theme.Translucent.NoActionBarAppCompat" >

            <intent-filter>      
                <category android:name="android.intent.category.DEFAULT" />
                <action android:name="mine.action.HomeActivity" />
                
                <category android:name="android.intent.category.LAUNCHER" />
                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
        </activity>
        
        
        
        <meta-data android:name="com.google.android.maps.v2.API_KEY"
                   android:value="xxxxxx" />
        
        <meta-data android:name="com.google.android.gms.version" 
                   android:value="@integer/google_play_services_version" />
        
        
        
        <uses-library android:name="com.google.android.maps" /> 
        
        
    
        
        <service android:name="com.my.engine.logic.EngineService" 
                 android:process=":myService">
            <intent-filter>
                
                <action android:name="com_my_RemoteService" />
                <action android:name="com_my_EngineService" />
            </intent-filter>
        </service>
        
        

        
                
        <receiver 
          android:name="com.my.engine.analytics.ReferrerReceiver" 
          android:exported="true" android:process=":myService">
            <intent-filter>
                <action 
              android:name="com.android.vending.INSTALL_REFERRER"/>
            </intent-filter>
        </receiver>
        
        <receiver 
              android:name="com.my.engine.BootBroadcastReceiver" 
              android:process=":myService">
            <intent-filter>
                <action 
android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>

        <receiver 
             android:name="com.my.engine.util.MineWatcherReceiver" 
             android:process=":myServiceWatcher">
            <intent-filter>
                <action
       android:name="android.intent.action.ACTION_POWER_CONNECTED" />
                <action   
       android:name="android.intent.action.BATTERY_OKAY" />
                <action 
       android:name="android.net.conn.CONNECTIVITY_CHANGE" />
                <action 
       android:name="android.intent.action.USER_PRESENT" />
            </intent-filter>
        </receiver>
    
        <receiver 
           android:name="com.my.engine.logic.InitServiceReceiver" 
           android:exported="true" android:process=":myService">
            <intent-filter>
                <action android:name="initServiceCheck"/>
            </intent-filter>
        </receiver>

        <receiver 
            android:name="com.my.engine.logic.UpdateCounterReceiver" 
            android:exported="true" android:process=":myService">
            <intent-filter>
                <action android:name="updateCustomCounter"/>
            </intent-filter>
        </receiver>
        
        <receiver 
         android:name="com.my.engine.logic.PackageChangeReceiver" 
         android:process=":myService">
            <intent-filter>
                <action  
              android:name="android.intent.action.PACKAGE_ADDED"/>
                <action 
              android:name="android.intent.action.PACKAGE_REPLACED"/>
                <action 
              android:name="android.intent.action.PACKAGE_REMOVED"/>
                <action 
              android:name="android.intent.action.PACKAGE_INSTALL"/>
                <data android:scheme="package"/>
            </intent-filter>
        </receiver>
        
        <!-- for notification try next action -->
        <service android:name="com.my.notifications.actions.TryNextNotificationActionService" />
        
        <receiver android:name="com.my.ui.base.EulaReminderReceiver">
            <intent-filter>
                <action android:name="eulaReminderAction" />         
            </intent-filter>
        </receiver>
        
        <receiver android:name="com.my.ui.base.MineGuiBroadcastReceiver">
            <intent-filter>
                <action android:name="finishStoppedActivities" /> 
            </intent-filter>
        </receiver>
        
        <service android:name="com.my.infra.motion.ActivityRecognitionService"
                 android:label="ActivityRecognitionService"
                 android:exported="true"
                 android:enabled="true"
                 android:process=":myService">
        </service>
        
    </application>
    
</manifest> 

编辑1

我甚至停止了设置的通知,但是前台仍然是process=":myService

编辑2

来自资料来源

代码语言:javascript
代码运行次数:0
运行
复制
if (p == SP_BACKGROUND)
        strcpy(proc->policy, "bg");
    else if (p == SP_FOREGROUND)
        strcpy(proc->policy, "fg");
    else
        strcpy(proc->policy, "er");

来自其他问题的答案

用于PCY的 PCY --策略--决定Android的内存管理器应该如何对待应用程序 前台进程被认为是前台进程,不应该被终止以释放内存。 BG -背景进程被认为是一个后台进程(不活跃地在前台运行,可能会被终止以释放内存)。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-09-21 19:25:24

详细阐述亚历克斯·P.的回答:

我相信PCY列是指流程分配给的cgroup。Android定义了两个cgroup组,SP_FOREGROUNDSP_BACKGROUNDSP_BACKGROUND的实际名称是bg_non_interactive。这些组分别用top表示,缩写为fgbg

您可以在整个框架中找到这些引用,特别是在Process.javaIPCThreadState.cppandroid_util_Process.cpp中的本机代码中,这些代码与Linux/proc文件系统接口,以管理正在运行的进程的各个方面。根据这些文件中注释的源代码,前台cgroup中的所有线程似乎都是用CPU的“正常”份额调度的,而后台cgroup中的线程则是用“缩减”份额调度的。

至于正常和简化的定义,博客指出,SP_BACKGROUND线程仅限于5%的CPU使用率。您可以通过查看正在运行的设备上的/dev/cpuctl/bg_non_interactive/cpu.shares来确认这一点。在我运行AOSP 5.1的Nexus 5上,我得到:

代码语言:javascript
代码运行次数:0
运行
复制
root@hammerhead:/ # cat /dev/cpuctl/bg_non_interactive/cpu.shares              
52
root@hammerhead:/ #

在这里,52是指cgroup中线程所允许的'CPU共享‘的数量,最多为1024个共享。因此,在这种情况下,确实允许bg_non_interactive线程对组中的所有线程最多使用5%的CPU。

无论如何,很明显前景和背景与安卓的活动生命周期以及前台和后台应用程序的概念没有什么关系。这就是安卓利用Linux的cgroups工具的方式。

票数 2
EN

Stack Overflow用户

发布于 2015-09-21 18:06:10

PCY代表scheduling policy。该列中的fg意味着进程获得比bg更高的优先级。这并不意味着进程正在前景中运行。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32701187

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档