2017-05-14 120 views
1

我創建了簡單的android application,其中包含一項活動和服務。該服務由aidl文件定義,並且可以通過Binder接收來自該活動的呼叫。一切都按預期工作。通過adb發送消息給定製服務

現在我部署(還我試着仿真器)設備上的應用程序,我想通過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發送消息給我的服務?

+0

似乎它的系統服務只有:http://stackoverflow.com/questions/12157442/android-emulator-how-can-i-get -a-list-of-services-that-are-running#comment63723794_24208845,我找不到我的服務以及使用'服務列表' – pskink

+0

,如果'adb shell startservice'工作正常,然後用它將消息傳遞給你的服務 – pskink

+0

@pskink,你能展示如何使用'startservice'傳遞消息嗎? – Gluttton

回答

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 user_id | current:指定運行哪個用戶;如果未指定,則以當前用戶身份運行。

看到這個link對於如何設置意圖作爲參數