adb shell am broadcast 参数
[-a <ACTION>]
[-p <PKG>]
[-d <DATA_URI>]
[-t <MIME_TYPE>]
[-c <CATEGORY> [-c <CATEGORY>] ...]
[-e|--es <EXTRA_KEY> <EXTRA_STRING_VALUE> ...]
[--ez <EXTRA_KEY> <EXTRA_BOOLEAN_VALUE> ...]
[--ei <EXTRA_KEY> <EXTRA_INT_VALUE> ...]
[--exclude-stopped-packages]
[--include-stopped-packages]
例如: adb shell am broadcast -a com.android.test -p com.android.test --es test_string "this is test string" --ei test_int 100 --ez test_boolean true 说明:蓝色为key,红色为alue,分别为String类型,int类型,boolean类型
在onCreat方法中先注册监听器
IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction("com.android.test"); //添加事件 MyReceiver myReceiver = new MyReceiver(); getActivity().registerReceiver(myReceiver, intentFilter);
实现MyReceiver
public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); String test_string = intent.getStringExtra("test_string"); if (test_string == null) { Log.e("com.android.test", "param test_string is null"); return; } if(action.equals("com.android.test")) { Log.e("com.android.test", "test_string: " + test_string); } } }
参考链接: