【问题标题】:Send message to custom service via adb通过 adb 向自定义服务发送消息
【发布时间】:2017-10-13 11:51:15
【问题描述】:

我创建了简单的 android application,其中包含一个活动和服务。该服务由aidl 文件定义,并且可以通过Binder 接收来自活动的调用。一切正常。

现在我在设备上部署应用程序(我也尝试过模拟器),我想通过adb向服务发送消息:

$ adb shell service call \
    com.example.gluttton.dummyandroid/.DummyService 0 STR "Bingo!"

并收到消息说此类服务不存在:

服务:服务 com.example.gluttton.dummyandroid/.DummyService 确实 不存在

我尝试检查我的服务是否启动:

$ adb shell service list

$ adb shell service check com.example.gluttton.dummyandroid/.DummyService

得到了类似的结果。

但与此同时,我使用dumpsys 看到我的服务:

$ adb shell dumpsys activity services Dummy
ACTIVITY MANAGER SERVICES (dumpsys activity services)
  User 0 active services:
  * ServiceRecord{a1627b5 u0 com.example.gluttton.dummyandroid/.DummyService}
    intent={act=android.intent.action.MAIN     cat=[android.intent.category.LAUNCHER]     cmp=com.example.gluttton.dummyandroid/.DummyService}
    packageName=com.example.gluttton.dummyandroid
    processName=com.example.gluttton.dummyandroid
    baseDir=/data/app/com.example.gluttton.dummyandroid-1/base.apk
    dataDir=/data/data/com.example.gluttton.dummyandroid
    app=ProcessRecord{114da04c 20168:com.example.gluttton.dummyandroid/u0a126}
    createTime=-21m40s817ms startingBgTimeout=--
    lastActivity=-21m40s816ms restartTime=-21m40s816ms createdFromFg=true
    startRequested=true delayedStop=false stopIfKilled=false callStart=true lastStartId=1

我也可以启动我的服务:

$ adb shell am startservice com.example.gluttton.dummyandroid/.DummyService
Starting service: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.gluttton.dummyandroid/.DummyService }

然后停止它:

$ adb shell am stopservice com.example.gluttton.dummyandroid/.DummyService
Stopping service: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.gluttton.dummyandroid/.DummyService }
Service stopped

所以我的问题是我的错误在哪里以及如何通过adb 向我的服务发送消息?

【问题讨论】:

  • 似乎它仅用于系统服务:stackoverflow.com/questions/12157442/…,我也无法使用service list找到我的服务
  • 如果adb shell am startservice 有效,则使用它将消息传递给您的服务
  • @pskink,你能展示如何使用startservice传递消息吗?

标签: android


【解决方案1】:

我认为 system/bin/service 仅用于使用 android 系统服务。例如,如果您执行adb -s emulator-5554 shell service list,您将只看到一个服务列表,例如

Found 100 services:
0   carrier_config: [com.android.internal.telephony.ICarrierConfigLoader]
1   phone: [com.android.internal.telephony.ITelephony]
2   isms: [com.android.internal.telephony.ISms]
3   iphonesubinfo: [com.android.internal.telephony.IPhoneSubInfo]

如果您想向您的服务发送消息,您可以通过

adb shell am startservice [options] intent

启动意图指定的服务。 有关意图参数,请参阅规范。

选项是:

--用户user_id | current:指定以哪个用户身份运行;如果未指定,则以当前用户身份运行。

查看link 了解如何将意图设置为参数

【讨论】:

    【解决方案2】:

    adb shell am start-foreground-service --user 0 -n com.varun.services/.wifiservice --ei "op" 1

    应用程序 wifiservice 中有一个服务,它将接受像 1 这样的输入,然后通过验证数据来执行操作

    服务代码如下

    这可能会有所帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-22
      • 2013-08-01
      • 1970-01-01
      • 2020-12-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多