這篇文章給大家介紹 BroadcastReceiver怎么在Android中使用,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
Android是一種基于Linux內核的自由及開放源代碼的操作系統,主要使用于移動設備,如智能手機和平板電腦,由美國Google公司和開放手機聯盟領導及開發。
主要代碼
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); } }
關于 BroadcastReceiver怎么在Android中使用就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。