Android BroadcastReceiver廣播簡單使用
本文實例為大傢分享瞭Android BroadcastReceiver廣播使用的具體代碼,供大傢參考,具體內容如下
靜態的BroadcastReceiver
主要代碼
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); } }
樣式
當然也可以實現不同app接受發送的廣播內容
復制代碼換app名字,當前app發送的廣播新的app也可以接收到
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。
推薦閱讀:
- None Found