Android的广播接收器

android的四大组件:

​ Activity、Service、BroadCast(广播)、ContentProvider

 

### BroadCast Receiver

- 四大组件之一,必须在清单(androidManifest.xml)文件中进行注册
- 是在组件之间传播数据(Intent)的一种机制
- APP哪块用到了广播?
- 电话
- 短信
- 电量
- WIFI是否连接

 

Android中出现的第一个错误?

​ 在组件中进行耗时操作,就会报application no response (ANR)

广播接收者一种有几种?

​ 标准广播

​ 动态注册 使用java代码注册

​ 静态注册 直接在androidManifest.xml注册

​ 有序广播

 

实现简单的静态注册

1. 创建广播接收者

2. 在AndroidManifest.xml中对广播接收者进行注册

3. 发送广播

实现简单的动态注册

 

 

 

 静态:

public class MainActivity7 extends AppCompatActivity {
private Button btn_gb;
private Button btn_dt;
private MyDyminicReceiver myDyminicReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main7);

btn_gb = findViewById(R.id.btn_gb);
btn_gb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//发送广播给静态广播接受者
Intent intent = new Intent();
intent.setPackage(getPackageName());
intent.setAction("houpu");
intent.putExtra("info","你好啊");
sendBroadcast(intent);

}
});

public class MainActivity8 extends AppCompatActivity {
private MyNetWorkReceiver myNetWorkReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main8);
//注册
myNetWorkReceiver = new MyNetWorkReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(myNetWorkReceiver,intentFilter);
}

@Override
protected void onDestroy() {
super.onDestroy();
//反注册
unregisterReceiver(myNetWorkReceiver);
}

class MyNetWorkReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
//接收到系统给提供的广播
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();//报错因为权限
if (networkInfo != null && networkInfo.isAvailable()){
if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI){
Toast.makeText(context,"WLFI",Toast.LENGTH_SHORT).show();
}else if (networkInfo.getType()==ConnectivityManager.TYPE_MOBILE){
Toast.makeText(context,"流量",Toast.LENGTH_SHORT).show();
}
}else {
Toast.makeText(context,"网络未连接",Toast.LENGTH_SHORT).show();
}
}
}
}




动态:
 btn_dt = findViewById(R.id.btn_dt);
btn_dt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("houpu1");
intent.putExtra("info","动态你好你好");
sendBroadcast(intent);
}
});
//注册动态广播接收者
myDyminicReceiver = new MyDyminicReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("houpu1");
registerReceiver(myDyminicReceiver,intentFilter);
}
class MyDyminicReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
Log.i("动态广播接收者",intent.getStringExtra("info"));
}
}

@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(myDyminicReceiver);
}





 

posted @ 2022-07-26 19:33  张帅贝  阅读(224)  评论(0编辑  收藏  举报