Android BroadcastReceiver廣播簡單使用
本文實(shí)例為大家分享了Android BroadcastReceiver廣播使用的具體代碼,供大家參考,具體內(nèi)容如下
靜態(tài)的BroadcastReceiver
主要代碼
public class MyReceiver extends BroadcastReceiver {
@Override
//接受廣播時(shí)回調(diào)
public void onReceive(Context context, Intent intent) {
//接收廣播
if(intent != null){
//接收到是什么廣播
String action = intent.getAction();
Log.e("測試",action);
}
}
}
在AndroidManifest.xml里設(shè)置權(quán)限
<receiver android:name=".MyReceiver">
<!--接受廣播類型-->
<intent-filter>
<!--開機(jī)廣播-->
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<!--電量低廣播-->
<action android:name="android.intent.action.BATTERY_LOW"/>
<!--應(yīng)用卸載-->
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<!--應(yīng)用安裝-->
<action android:name="android.intent.action.PACKAGE_INSTALL"/>
<!--數(shù)據(jù)類型-->
<data android:scheme="package"/>
</intent-filter>
</receiver>
動(dòng)態(tài)的BroadcastReceiver
主要代碼
1.設(shè)置一個(gè)Java類繼承BroadcastReceiver
public class MyReceiverD extends BroadcastReceiver {
@Override
//接受廣播時(shí)回調(diào)(不能做耗時(shí)操作,必須開子線程)
public void onReceive(Context context, Intent intent) {
//接收廣播
if(intent != null){
//接收到是什么廣播
String action = intent.getAction();
Log.e("測試",action);
}
}
}
在AndroidManifest.xml里設(shè)置權(quán)限
<!--動(dòng)態(tài)注冊-->
<receiver android:name=".MyReceiverD">
//因?yàn)槭莿?dòng)態(tài)設(shè)置就不需要在里面設(shè)置別的了
</receiver>
3.MainActivity
//新建一個(gè)廣播接收器 動(dòng)態(tài)廣播
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();
//取消注冊關(guān)閉接收器
if (receiverD != null){
unregisterReceiver(receiverD);
}
}
隨便卸載一個(gè)應(yīng)用控制臺(tái)就會(huì)顯示

自定義的BroadcastReceiver
1.還是準(zhǔn)備一個(gè)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)){
//獲取廣播攜帶的數(shù)據(jù)
String content = intent.getStringExtra(MainActivity.BROADCAST_CONTENT);
if (txt != null){
txt.setText("接收到的action是:"+action+"\n接收到的內(nèi)容是"+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="請輸入發(fā)送內(nèi)容:"/>
<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="發(fā)送廣播"/>
<TextView
android:id="@+id/txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="收到的內(nèi)容:"/>
</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();
//設(shè)置應(yīng)用主頁面的標(biāo)題
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);
//攜帶數(shù)據(jù)
intent.putExtra(BROADCAST_CONTENT,etxt.getText().toString());
//發(fā)送廣播
sendBroadcast(intent);
}
});
}
protected void onDestroy() {
super.onDestroy();
//取消注冊關(guān)閉接收器
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);
}
}
樣式

當(dāng)然也可以實(shí)現(xiàn)不同app接受發(fā)送的廣播內(nèi)容
復(fù)制代碼換app名字,當(dāng)前app發(fā)送的廣播新的app也可以接收到

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android 逆向?qū)W習(xí)詳解及實(shí)例
本文主要介紹Android 逆向?qū)W習(xí),這里整理逆向?qū)W習(xí)的思路及學(xué)習(xí)要點(diǎn),并附示例代碼,幫助大家學(xué)習(xí)理解,有需要的小伙伴可以參考下2016-09-09
Android使用SurfaceView實(shí)現(xiàn)飄贊動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了Android如何使用SurfaceView實(shí)現(xiàn)飄贊動(dòng)畫,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
Android邊框裁切的正確姿勢實(shí)現(xiàn)示例
這篇文章主要為大家介紹了Android邊框裁切的正確姿勢實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
Android Studio配置(Android Studio4.1為例)
這篇文章主要介紹了Android Studio配置(Android Studio4.1為例),文中通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
Android編程實(shí)現(xiàn)禁止StatusBar下拉的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)禁止StatusBar下拉的方法,涉及Android StatusBarManager相關(guān)屬性控制操作技巧,需要的朋友可以參考下2017-08-08

