Android BroadcastReceiver广播简单使用
本文实例为大家分享了Android BroadcastReceiver广播使用的具体代码,供大家参考,具体内容如下
静态的BroadcastReceiver
主要代码
public class MyReceiver extends BroadcastReceiver {
@Override
//接受广播时回调
public void onReceive(Context context, Intent intent) {
//接收广播
if(intent != null){
//接收到是什么广播
String action = intent.getAction();
Log.e("测试",action);
}
}
}
在AndroidManifest.xml里设置权限
<receiver android:name=".MyReceiver">
<!--接受广播类型-->
<intent-filter>
<!--开机广播-->
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<!--电量低广播-->
<action android:name="android.intent.action.BATTERY_LOW"/>
<!--应用卸载-->
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<!--应用安装-->
<action android:name="android.intent.action.PACKAGE_INSTALL"/>
<!--数据类型-->
<data android:scheme="package"/>
</intent-filter>
</receiver>
动态的BroadcastReceiver
主要代码
1.设置一个Java类继承BroadcastReceiver
public class MyReceiverD extends BroadcastReceiver {
@Override
//接受广播时回调(不能做耗时操作,必须开子线程)
public void onReceive(Context context, Intent intent) {
//接收广播
if(intent != null){
//接收到是什么广播
String action = intent.getAction();
Log.e("测试",action);
}
}
}
在AndroidManifest.xml里设置权限
<!--动态注册-->
<receiver android:name=".MyReceiverD">
//因为是动态设置就不需要在里面设置别的了
</receiver>
3.MainActivity
//新建一个广播接收器 动态广播
receiverD = new MyReceiverD();
//接收那种广播
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
intentFilter.addDataScheme("package");
intentFilter.addAction(Intent.ACTION_BATTERY_LOW);
//注册广播接收器
registerReceiver(receiverD,intentFilter);
protected void onDestroy() {
super.onDestroy();
//取消注册关闭接收器
if (receiverD != null){
unregisterReceiver(receiverD);
}
}
随便卸载一个应用控制台就会显示
自定义的BroadcastReceiver
1.还是准备一个Java继承BroadcastReceiver
public class MyReceiverD_zdy extends BroadcastReceiver {
private TextView txt;
public MyReceiverD_zdy(TextView txt) {
this.txt = txt;
}
public MyReceiverD_zdy() {
}
@Override
public void onReceive(Context context, Intent intent) {
//接收广播
if(intent != null){
//接收到是什么广播
String action = intent.getAction();
Log.e("测试",action);
//判断是什么广播,是否是自己自定义的广播
if (TextUtils.equals(action,MainActivity.MY_ACTION)){
//获取广播携带的数据
String content = intent.getStringExtra(MainActivity.BROADCAST_CONTENT);
if (txt != null){
txt.setText("接收到的action是:"+action+"\n接收到的内容是"+content);
}
}
}
}
}
2.activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:padding="16dp"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请输入发送内容:"/>
<EditText
android:id="@+id/etxt"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="16dp"
/>
<Button
android:id="@+id/bnt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_gravity="center_horizontal"
android:text="发送广播"/>
<TextView
android:id="@+id/txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="收到的内容:"/>
</LinearLayout>
3.MainActivity
public class MainActivity extends AppCompatActivity {
private MyReceiverD receiverD;
private MyReceiverD_zdy receiverDZdy;
private Button bnt;
private EditText etxt;
private TextView txt;
public static final String MY_ACTION = "com.example.my";
public static final String BROADCAST_CONTENT = "cs";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
//设置应用主页面的标题
setTitle(getPackageName());
//新建广播接收器
receiverDZdy = new MyReceiverD_zdy(txt);
//注册广播接收器
//为广播添加Action
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("android.intent.action,PACKAGE_REMOVED");
//自定义
intentFilter.addAction(MY_ACTION);
//注册广播接收器
registerReceiver(receiverDZdy,intentFilter);
bnt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//新建广播 自定义
Intent intent = new Intent(MY_ACTION);
//携带数据
intent.putExtra(BROADCAST_CONTENT,etxt.getText().toString());
//发送广播
sendBroadcast(intent);
}
});
}
protected void onDestroy() {
super.onDestroy();
//取消注册关闭接收器
if (receiverDZdy != null){
unregisterReceiver(receiverDZdy);
}
}
private void initView() {
//初始化
etxt = (EditText) findViewById(R.id.etxt);
txt =(TextView) findViewById(R.id.txt);
bnt =(Button) findViewById(R.id.bnt);
}
}
样式
当然也可以实现不同app接受发送的广播内容
复制代码换app名字,当前app发送的广播新的app也可以接收到
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
以上是 Android BroadcastReceiver广播简单使用 的全部内容, 来源链接: utcz.com/p/243433.html