49-Android之user版本不能执行adb_shell_am_start的问题

本文介绍了解决Android用户版本中无法使用adb shell am start命令的问题。通过修改ActivityManager.java文件中的权限检查逻辑,使得在user版本下也能正常执行该命令。

49-Android之user版本不能执行adb_shell_am_start的问题


平台: Android10 MTK6739

客户测试发现,在user版本无法执行adb shell am start指令,提示没有权限,但是在eng版本中是可以正常执行的。

因此,要求需要在user版本中也能执行adb shell am start指令。

frameworks/base/core/java/android/app/ActivityManager.java    

    @UnsupportedAppUsage
    public static int checkComponentPermission(String permission, int uid,
            int owningUid, boolean exported) {
        // Root, system server get to do everything.
        final int appId = UserHandle.getAppId(uid);
        // TODO 修改此处, 添加SHELL_UID的判断
        if (appId == Process.ROOT_UID || appId == Process.SYSTEM_UID /*TODO add begin*/|| appId == Process.SHELL_UID/*TODO add end*/) {
            return PackageManager.PERMISSION_GRANTED;
        }
        // Isolated processes don't get any permissions.
        if (UserHandle.isIsolated(uid)) {
            return PackageManager.PERMISSION_DENIED;
        }
        // If there is a uid that owns whatever is being accessed, it has
        // blanket access to it regardless of the permissions it requires.
        if (owningUid >= 0 && UserHandle.isSameApp(uid, owningUid)) {
            return PackageManager.PERMISSION_GRANTED;
        }
        // ....
    }
<think>我们被要求使用adb shell am start命令,特别是带-w选项来启动一个Android应用组件并等待其完成。 根据引用[1]:adb启动程序命令:adb shell am start。但没有详细说明-w选项。 我们需要更详细的信息:-w选项用于等待启动完成,并打印启动时间。 实际上,am start命令的完整用法如下(通过adb shell am start -h可以查看): start [options] <INTENT> options包括: -D: 启用调试 -W: 等待启动完成 --start-profiler <FILE>: 启动分析器并将结果发送到<FILE> -P <FILE>: 类似--start-profiler,但分析器在应用进入空闲状态时停止 -R <COUNT>: 重复执行活动启动<COUNT>次 -S: 在启动Activity前强行停止目标应用 --opengl-trace: 启用OpenGL函数的跟踪 --user <USER_ID> | current: 指定要作为哪个用户运行;如果未指定,则作为当前用户运行 因此,-W选项就是用来等待启动完成的。 示例:启动包名为com.example.app的应用的主Activity,并等待其完成: adb shell am start -W com.example.app/.MainActivity 输出会包含: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.app/.MainActivity } Status: ok Activity: com.example.app/.MainActivity ThisTime: 100 TotalTime: 100 WaitTime: 150 Complete 其中,ThisTime: 最后一个Activity启动耗时; TotalTime: 所有Activity启动耗时; WaitTime: AMS启动Activity的总耗时。 注意:Intent可以通过组件名指定,也可以通过Action和Category等指定。 例如,通过Action和Category启动: adb shell am start -W -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -n com.example.app/.MainActivity 但是,如果应用已经启动,可能不会等待到界面完全绘制(因为Activity已经存在,只是被带到前台)。为了确保启动新的实例,可以添加标志FLAG_ACTIVITY_NEW_TASK和FLAG_ACTIVITY_CLEAR_TOP等,但通常am start会默认添加FLAG_ACTIVITY_NEW_TASK。 另外,如果应用尚未安装或组件不存在,命令会报错。 因此,针对用户的问题,我们给出使用-W选项的示例,并解释输出。 注意:如果启动的Activity是应用的主Activity,通常我们使用包名/主Activity的完整类名(如com.example.app/.MainActivity),其中MainActivity前面的点表示包名后面的类名。 如果不知道主Activity,可以通过以下方式获取: adb shell dumpsys package <package_name> | grep -A 1 "android.intent.action.MAIN" 但这不是必须的,因为通常开发者知道主Activity。 另外,引用[3]中提到了input命令,但这里不相关。 根据引用[2]中dumpsys的用法,可以用于获取网络状态等信息,但和启动应用无关。 因此,我们聚焦在am start -w的使用。 下面我们给出具体步骤和示例。</think>### 启动Android应用组件并等待完成的adb命令详解 `adb shell am start -w`命令用于启动Android应用组件并等待其完全启动完成。其中`-w`参数是关键,它会强制命令等待直到Activity完全启动并显示在屏幕上才返回结果[^1]。 #### 命令基本格式 ```bash adb shell am start -W [选项] <组件名> ``` #### 核心参数说明 1. **`-W`(必选)** 等待启动完成,并输出启动耗时统计: ```bash adb shell am start -W com.example.app/.MainActivity ``` 输出示例: ``` Status: ok Activity: com.example.app/.MainActivity ThisTime: 250 # 当前Activity启动耗时 TotalTime: 400 # 包括依赖资源的整体耗时 WaitTime: 600 # AMS(Activity Manager Service)总处理时间 ``` 2. **组件名格式** `<包名>/<完整Activity类名>` 例如:`com.tencent.mm/.ui.LauncherUI` 3. **常用附加选项** - `-n`:显式指定组件名 ```bash adb shell am start -W -n com.example.app/.MainActivity ``` - `-a`:指定Action(如主屏启动) ```bash adb shell am start -W -a android.intent.action.MAIN ``` - `-c`:添加Category ```bash adb shell am start -W -c android.intent.category.LAUNCHER ``` - `-f`:设置Intent Flags ```bash adb shell am start -W -f 0x10200000 ``` #### 典型使用场景 1. **冷启动性能测试** ```bash # 先强制停止应用 adb shell am force-stop com.example.app # 冷启动并测量时间 adb shell am start -W -n com.example.app/.MainActivity ``` 2. **带参数启动Activity** ```bash adb shell am start -W -a android.intent.action.VIEW \ -d "https://example.com" com.example.app/.WebViewActivity ``` 3. **调试模式启动** ```bash adb shell am start -W -D -n com.example.app/.DebugActivity ``` #### 注意事项 1. 确保设备已开启USB调试(`adb devices`验证连接) 2. 组件名必须完全匹配(可通过`adb shell dumpsys package <包名>`查询) 3. 当出现`Error: Activity not started`时,检查: - 组件名是否拼写错误 - Activity是否在AndroidManifest.xml中导出 - 是否缺少必要权限[^3] > 通过`-w`参数获取的启动时间(`TotalTime`)是性能优化的重要指标,常用于自动化测试脚本中[^1]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值