一概述
本地廣播事件的發(fā)送和接收都在本應(yīng)用胖秒,不影響其他應(yīng)用也不受其他應(yīng)用影響,只能被動態(tài)注冊超歌,不能靜態(tài)注冊补胚,主要用法都在LocalBroadcastManager類中悯衬,而全局廣播則是在Android設(shè)備上弹沽,主要注冊了就都能收到。
使用
1筋粗、全局廣播Android中使用
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tvSend"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="20dp"
android:background="#aeaeae"
android:gravity="center"
android:text="發(fā)送廣播"
android:textColor="#000000" />
<TextView
android:id="@+id/tvReceiver"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="展示接受的廣播信息"
android:textColor="#000000" />
</LinearLayout>
Java中發(fā)送和注冊的代碼
public class NormalBroadCastActivity extends Activity {
private TextView tvReceiver;
private final String ACTION_TEST = "demos.android.stormdzh.com.androiddemos.broadcast.NormalBroadCastActivity.TestBroadcastReceiver";
private int index =0;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.adapter_broadcast_normal);
tvReceiver = findViewById(R.id.tvReceiver);
findViewById(R.id.tvSend).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
send("參數(shù):"+index);
index++;
}
});
//注冊廣播
registerBroadCast();
}
private void send(String param) {
try {
Intent intent = new Intent();
intent.putExtra("param", param);
intent.setAction(ACTION_TEST);
sendBroadcast(intent);//發(fā)送標(biāo)準(zhǔn)廣播
} catch (Exception e) {
e.printStackTrace();
}
}
private TestBroadcastReceiver mTestBroadcastReceiver;
private void registerBroadCast() {
try {
//注冊點擊事件廣播
IntentFilter filter = new IntentFilter(ACTION_TEST);
filter.setPriority(Integer.MAX_VALUE); //設(shè)置級別
mTestBroadcastReceiver = new TestBroadcastReceiver();
registerReceiver(mTestBroadcastReceiver, filter);
} catch (Exception e) {
e.printStackTrace();
}
}
public class TestBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent == null) return;
if (!TextUtils.equals(intent.getAction(), ACTION_TEST)) {
return;
}
final String param = intent.getStringExtra("param");
Log.i("dzh", " 廣播里面收到參數(shù):" + param);
runOnUiThread(new Runnable() {
@Override
public void run() {
tvReceiver.setText(" 廣播里面收到參數(shù):" + param);
}
});
}
}
}
2策橘、本地廣播Android中使用
由于xml文件都是一致的,就不重復(fù)寫了,核心是使用了LocalBroadcastManager娜亿,下面直接貼上java代碼
public class LocalBroadCastActivity extends Activity {
private TextView tvReceiver;
private final String ACTION_TEST = "demos.android.stormdzh.com.androiddemos.broadcast.NormalBroadCastActivity.TestBroadcastReceiver";
private int index =0;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.adapter_broadcast_normal);
tvReceiver = findViewById(R.id.tvReceiver);
findViewById(R.id.tvSend).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
send("參數(shù):"+index);
index++;
}
});
//注冊廣播
registerBroadCast();
}
private void send(String param) {
try {
Intent intent = new Intent();
intent.putExtra("param", param);
intent.setAction(ACTION_TEST);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);//發(fā)送標(biāo)準(zhǔn)廣播
} catch (Exception e) {
e.printStackTrace();
}
}
private TestBroadcastReceiver mTestBroadcastReceiver;
private void registerBroadCast() {
try {
//注冊點擊事件廣播
IntentFilter filter = new IntentFilter(ACTION_TEST);
filter.setPriority(Integer.MAX_VALUE); //設(shè)置級別
mTestBroadcastReceiver = new TestBroadcastReceiver();
LocalBroadcastManager.getInstance(this).registerReceiver(mTestBroadcastReceiver, filter);
} catch (Exception e) {
e.printStackTrace();
}
}
public class TestBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent == null) return;
if (!TextUtils.equals(intent.getAction(), ACTION_TEST)) {
return;
}
final String param = intent.getStringExtra("param");
Log.i("dzh", " 廣播里面收到參數(shù):" + param);
runOnUiThread(new Runnable() {
@Override
public void run() {
tvReceiver.setText(" 廣播里面收到參數(shù):" + param);
}
});
}
}
}
三丽已、總結(jié)
簡單的使用就是這么多,接下來有空研究下廣播的實現(xiàn)原理买决。