以版本不可知的方式从命令行启动Android服务



4.0.4设备在没有--user选项的情况下会成功启动服务器,但会失败。4.2.1设备需要--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)

在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'])

最新更新