【问题标题】:Start an Android Service from Command Line in Version-Agnostic way以与版本无关的方式从命令行启动 Android 服务
【发布时间】:2013-06-19 06:17:09
【问题描述】:

4.0.4 设备将在没有 --user 选项的情况下成功启动服务器,但会失败。 4.2.1 设备需要 --user 选项并且没有它会失败。

如何以适用于所有 Android 设备的方式启动服务?

或者如果不可能,确定何时需要 --user 的标准是什么?

$ adb shell getprop ro.build.version.release
4.0.4

$ adb shell am startservice --user 0 -ei 123 -a com.a.activity.myservice
Error: Unknown option: --user
usage: am [subcommand] [options] 
   { usage info deleted

$ adb shell getprop ro.build.version.release
4.2.1

$ adb shell am startservice  -ei 123 -a com.a.activity.myservice
Starting service: Intent { act=com.a.activity.myervice (has extras) }
~/dev/dcf/Applications/Spin (master) $ adb shell am startservice  -ei 123 -a com.a.activity.myservice
Starting service: Intent { act=com.apportable.a.myservice (has extras) }
java.lang.SecurityException: Caller uid=2000 is not privileged to communicate with user=-2
    at android.os.Parcel.readException(Parcel.java:1425)

【问题讨论】:

  • 一点点错误处理有什么问题?运行一个命令并处理输出,如果它失败 - 运行另一个。或者先不带参数运行am,然后检查--user 是否是可用选项
  • 好的,我希望我遗漏了一些东西,并且有一个更清洁的解决方案。

标签: android command-line android-service adb


【解决方案1】:

在Alex的建议下,我写了如下python片段:

    results = env.Execute([env['ADB'], 'shell', 'am'])
    if "--user" in results:
        user_option = "--user 0"
    else:
        user_option = ""

    exec_line = 'am startservice ' + user_option + ' -a ' + app_id + '.GdbServerService --es command start --es gdbserver_name ' + remote_gdbserver + ' --ei gdbserver_port ' + str(env['ANDROID_REMOTE_DEBUG_PORT'])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-23
    • 2014-10-18
    • 1970-01-01
    • 2015-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多