开启监听广播
//IntentFilter 意图筛选 筛选我需要接收哪些广播
IntentFilter intentFilter=new IntentFilter();
//com.skypan.updatehe和dongzuo2 接收广播的名字
intentFilter.addAction("com.skypan.update");
intentFilter.addAction("dongzuo2");
//开始接收广播
LocalBroadcastManager.getInstance(this).registerReceiver(myBroadcast,intentFilter);
处理广播
//广播类
private class MyBroadcast extends BroadcastReceiver{
//接受广播要干啥
@Override
public void onReceive(Context context, Intent intent) {
// intent.getAction 获取动作
switch (intent.getAction()){
case "com.skypan.update":
String name = intent.getStringExtra("name");
ToastUtil.showMsg(BroadActivity.this,"我是com.skypan.update在BroadActivity----"+name);
t1.setText(name);
break;
case "dongzuo2":
String name1 = intent.getStringExtra("name");
ToastUtil.showMsg(BroadActivity.this,"我是dongzuo2在BroadActivity---"+name1);
t1.setText(name1);
break;
}
}
}
页面销毁广播
@Override
protected void onDestroy() {
super.onDestroy();
LocalBroadcastManager.getInstance(this).unregisterReceiver(myBroadcast);//销毁广播
}
发送广播(带参数)
Intent intent=new Intent("com.skypan.update");
intent.putExtra("name","我是广播");
LocalBroadcastManager.getInstance(Broad2Avtivity.this).sendBroadcast(intent);
示例1
package com.example.myapplicationstyud2;
import androidx.appcompat.app.AppCompatActivity;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import com.example.textview.R;
import com.example.textview.Util.ToastUtil;
public class BroadActivity extends AppCompatActivity {
private TextView t1;
private MyBroadcast myBroadcast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_broad);
t1=(TextView) findViewById(R.id.t1);
myBroadcast=new MyBroadcast();
//IntentFilter 意图筛选 筛选我需要接收哪些广播
IntentFilter intentFilter=new IntentFilter();
//com.skypan.updatehe和dongzuo2 接收广播的名字
intentFilter.addAction("com.skypan.update");
intentFilter.addAction("dongzuo2");
//开始接收广播
LocalBroadcastManager.getInstance(this).registerReceiver(myBroadcast,intentFilter);
}
public void clickOn(View v){
switch (v.getId()){
case R.id.b1:
Intent intent=new Intent(this,Broad2Avtivity.class);
startActivity(intent);
break;
case R.id.b2:
break;
case R.id.b3:
break;
}
}
//广播类
private class MyBroadcast extends BroadcastReceiver{
//接受广播要干啥
@Override
public void onReceive(Context context, Intent intent) {
// intent.getAction 获取动作
switch (intent.getAction()){
case "com.skypan.update":
String name = intent.getStringExtra("name");
ToastUtil.showMsg(BroadActivity.this,"我是com.skypan.update在BroadActivity----"+name);
t1.setText(name);
break;
case "dongzuo2":
String name1 = intent.getStringExtra("name");
ToastUtil.showMsg(BroadActivity.this,"我是dongzuo2在BroadActivity---"+name1);
t1.setText(name1);
break;
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
LocalBroadcastManager.getInstance(this).unregisterReceiver(myBroadcast);//销毁广播
}
}
相关发送代码上面有
示例2 监听网络变化
public class MainActivity extends AppCompatActivity {
private IntentFilter intentFilter;
private NetworkChangeReceiver networkChangeReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//动态接受网络变化的广播接收器
intentFilter = new IntentFilter();
intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
networkChangeReceiver = new NetworkChangeReceiver();
registerReceiver(networkChangeReceiver, intentFilter);
}
@Override
protected void onDestroy() {
super.onDestroy();
//取消动态网络变化广播接收器的注册
unregisterReceiver(networkChangeReceiver);
}
//自定义接受网络变化的广播接收器
class NetworkChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connectionManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectionManager.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isAvailable()) {
Toast.makeText(context, "network is available", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "network is unavailable", Toast.LENGTH_SHORT).show();
}
}
}
}