您的位置:首頁技術文章
文章詳情頁

Android BroadcastReceiver廣播簡單使用

瀏覽:56日期:2022-09-19 10:07:21

本文實例為大家分享了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);} }

隨便卸載一個應用控制臺就會顯示

Android BroadcastReceiver廣播簡單使用

自定義的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' > <TextViewandroid:layout_width='match_parent'android:layout_height='wrap_content'android:text='請輸入發送內容:'/> <EditTextandroid: android:layout_width='match_parent'android:layout_height='50dp'android:layout_marginTop='16dp'/> <Buttonandroid: android:layout_width='match_parent'android:layout_height='wrap_content'android:layout_marginTop='16dp'android:layout_gravity='center_horizontal'android:text='發送廣播'/> <TextViewandroid: 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);//注冊廣播接收器//為廣播添加ActionIntentFilter 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); }}

樣式

Android BroadcastReceiver廣播簡單使用

當然也可以實現不同app接受發送的廣播內容復制代碼換app名字,當前app發送的廣播新的app也可以接收到

Android BroadcastReceiver廣播簡單使用

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Android
相關文章:
国产综合久久一区二区三区