亞行

2014-03-25 52 views
23

發送意圖廣播接收器我有廣播接收器類:亞行

public class IntentReceiver extends BroadcastReceiver { 

    final String tag = "Intent Intercepter"; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     try { 
      String data = intent.getStringExtra("sms_body"); 
      Log.i(tag, data); 
      Toast.makeText(context, data.subSequence(0, data.length()), Toast.LENGTH_LONG).show(); 
     } catch (Exception e) { 
      Toast.makeText(context, "Intercepted", Toast.LENGTH_LONG).show(); 
     } 
    } 
} 

而且還表現在:

<receiver android:name="com.whereismywifeserver.IntentReceiver" android:enabled="true"> 
    <intent-filter android:priority="999"> 
     <action android:name="com.whereismywifeserver.intent.TEST"/> 
    </intent-filter> 
</receiver> 

但是,當我嘗試從亞行派的意圖,我收到錯誤:

C:\Users\i.yesilevsky>adb shell am start -a com.whereismywifeserver.intent.TEST 
--es sms_body "test from adb" -c android.intent.category.HOME -n com.whereismywifeserver/.IntentReceiver 
Starting: Intent { act=com.whereismywifeserver.intent.TEST t=[android.intent.category.HOME] cmp=com.whereismywifeserver/.IntentReceiver (has extras) } 
Error type 3 
Error: Activity class {com.whereismywifeserver/com.whereismywifeser 
ver.IntentReceiver} does not exist. 

當我在代碼中創建意圖時,一切正常。那麼我怎麼能從adb發送意圖呢?

+0

可能重複(http://stackoverflow.com/questions/10277796/send-intent-to-app-in-emulator) – janot

+73

你終於找到她嗎? – Jmorvan

回答

18

我發現該命令是錯誤的,正確的命令中包含「廣播」,而不是「開始」:

adb shell am broadcast -a com.whereismywifeserver.intent.TEST --es sms_body "test from adb" -n com.whereismywifeserver/.IntentReceiver 
58

無需指定接收器。試試這個:

adb shell am broadcast -a com.whereismywifeserver.intent.TEST --es sms_body "test from adb" 
的[發送意向到App在模擬器]
+0

由於某些原因,如果字符串額外(如上例中的'來自adb'的測試)包含空格,廣播無法到達接收者。否則,它的作品。 – user2137020