通过adb将消息发送到自定义服务

时间:2017-05-14 17:28:37

标签: android

我创建了简单的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向我的服务发送消息?

2 个答案:

答案 0 :(得分: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

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

选项包括:

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

请参阅此link了解如何将意图设置为参数

答案 1 :(得分:0)

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

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

服务代码在下面

enter image description here

这可能会有所帮助