Android中使用命令 adb shell am broadcast 发送广播。
代码:
注册一个广播接收器,用来监听广播,如果收到了自己监听到的广播,则打印一条log:
public class MyBroadcastReceiverextends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("MyBroadcastReceiver","onReceive: start...");
throw new UnsupportedOperationException("Not yet implemented");
}
}
监听的广播内容在AndroidManifest.xml设置:
让广播接收器MyBroadcastReceiver去监听一条com.chen.broadcasttest.MY_BROADCAST的广播
android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
MainActivity只打印一条log
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {