android接收adb发送的系统广播及自定义广播

来源:互联网 发布:网络教育最快毕业几年 编辑:程序博客网 时间:2024/04/29 10:15

adb发送广播

adb shell am broadcast -a action的名字,即配置文件中action的名字

1、接收adb发送的系统的系统广播----屏幕变亮

①在程序中写一个广播接收者接收改广播

public class ScreeOnBroadcastReceiver extends BroadcastReceiver {

 @Override
 public void onReceive(Context context, Intent intent) {
  System.out.println("shou dao le guang bo");
  Toast.makeText(context, "接收到了系统屏幕变亮的广播数据", Toast.LENGTH_SHORT).show();
 }

}

②在配置文件注册改广播

 <receiver android:name="com.example.testbroadcast.ScreeOnBroadcastReceiver">
            <intent-filter >
                <action android:name="android.intent.action.ACTION_SCREEN_ON"/>
            </intent-filter>

 </receiver>

③在adb中输入命令

adb shell am broadcast -a android.intent.action.ACTION_SCREEN_ON

在程序中可收到发送的该广播

 

2、接收adb发送的系统广播------开机启动

①public class BootBroadcastReceiver extends BroadcastReceiver {

 @Override
 public void onReceive(final Context context, Intent intent) {
  Toast.makeText(context, "收到了系统开机启动的广播", Toast.LENGTH_SHORT).show();
  
 }
}

②在配置文件注册改广播

 <receiver android:name="com.example.testbroadcast.BootBroadcastReceiver">
            <intent-filter >
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
</receiver>

③在adb中输入命令

adb shell am broadcast -a android.intent.action.BOOT_COMPLETED

在程序中可收到发送的该广播

注:3.0以上输入该命令,手机会重新启动后需运行程序

 

3、接收adb发送的自定义广播

①public class MyBroadcastReceiver extends BroadcastReceiver {

 @Override
 public void onReceive(Context context, Intent intent) {
  System.out.println("shou dao le guang bo");
  Toast.makeText(context, "接收到了自定义的广播数据", Toast.LENGTH_SHORT).show();
 }

}

②在配置文件注册改广播

<receiver android:name="com.example.testbroadcast.MyBroadcastReceiver">
      <intent-filter >
            <action android:name="mybroadcast"/>
       </intent-filter>
</receiver>

③在adb中输入命令

adb shell am broadcast -a mybroadcast

在程序中可收到发送的该广播

注:“mybroadcast”为配置文件中注册的action的名称

 

 demo地址:http://download.csdn.net/detail/nicolelili1/8228449
0 0
原创粉丝点击