adb發(fā)送廣播
adb shell am broadcast -a action的名字澎灸,即配置文件中action的名字
1、接收adb發(fā)送的系統(tǒng)的系統(tǒng)廣播----屏幕變亮
①在程序中寫一個廣播接收者接收改廣播
public class ScreeOnBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("shou dao le guang bo");
Toast.makeText(context, "接收到了系統(tǒng)屏幕變亮的廣播數(shù)據(jù)", Toast.LENGTH_SHORT).show();
}
}
②在配置文件注冊改廣播
Android:name="com.example.testbroadcast.ScreeOnBroadcastReceiver">
③在adb中輸入命令
adb shell am broadcast -a android.intent.action.ACTION_SCREEN_ON
在程序中可收到發(fā)送的該廣播
2、接收adb發(fā)送的系統(tǒng)廣播------開機(jī)啟動
①public class BootBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
Toast.makeText(context, "收到了系統(tǒng)開機(jī)啟動的廣播", Toast.LENGTH_SHORT).show();
}
}
②在配置文件注冊改廣播
③在adb中輸入命令
adb shell am broadcast -a android.intent.action.BOOT_COMPLETED
在程序中可收到發(fā)送的該廣播
注:3.0以上輸入該命令,手機(jī)會重新啟動后需運(yùn)行程序
3、接收adb發(fā)送的自定義廣播
①public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("shou dao le guang bo");
Toast.makeText(context, "接收到了自定義的廣播數(shù)據(jù)", Toast.LENGTH_SHORT).show();
}
}
②在配置文件注冊改廣播
③在adb中輸入命令
adb shell am broadcast -a mybroadcast
在程序中可收到發(fā)送的該廣播
注:“mybroadcast”為配置文件中注冊的action的名稱