日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

广播接收者

發布時間:2025/3/21 编程问答 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 广播接收者 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、廣播發送者&廣播接收者介紹



1.廣播接收者


? ? 廣播接收者簡單地說就是接收廣播意圖的Java類,此Java類繼承BroadcastReceiver類,重寫:

? ? public void onReceive(Context context,Intent intent),其中intent可以獲得傳遞的數據;

? ? 廣播意圖就是通過Context.sendBroadcast(Intent intent)或Context.sendOrderedBroadcast(Intent intent)發送的意圖,通過這個語句,能夠廣播給所有滿足條件的組件,比如intent設置了action="com.xiazdong",則所有在AndroidManifest.xml中設置過<action android:name="com.xiazdong"/>的廣播接收者都能夠接收到廣播;

? ??

? ? 注:onReceive方法必須在10秒內完成,如果沒有完成,則拋“Application No Response”當廣播接收者onReceive方法需要執行很長時間時,最好將此耗時工作通過Intent發送給Service,由Service完成,并且不能使用子線程解決,因為BroadcastReceiver是接收到廣播后才創建的,并且生命周期很短,因此子線程可能在沒有執行完就已經被殺死了。

? ??

? ??

? ? public void onReceive(Context context,Intent intent){ ?

? ? ? ? Intent intent = new Intent(context,XxxService.class); ?

? ? ? ? context.startService(intent); ?

? ? } ?



2.廣播發送者


? ? 通常廣播發送方就是調用Context.sendBroadcast()的程序,而廣播接收者就是繼承BroadcastReceiver的程序;

? ? 通常廣播發送方都是通過隱式意圖,這樣才能發送給多人;

? ??

? ? 廣播發送方分為普通廣播和有序廣播;

? ? 普通廣播:發送方發出后,幾乎同時到達多個廣播接收者處,某個接收者不能接收到廣播后進行一番處理后傳給下一個接收者,并且無法終止廣播繼續傳播;Context.sendBroadcast(intent);

? ? 有序廣播:廣播接收者需要提前設置優先級,優先級高的先接收到廣播,優先級數值為-1000~1000,在AndroidManifest.xml的<intent-filter android:priority="xxx">設置;比如存在3個廣播接收者A、B、C,優先級A>B>C,因此A最先收到廣播,當A收到廣播后,可以向廣播中添加一些數據給下一個接收者(intent.putExtra()),或者終止廣播(abortBroadcast());

? ? Context.sendOrderedBroadcast(intent);



二、廣播接收者核心代碼

? ? 同步廣播發送方核心代碼:

? ? ? ? Intent intent = new Intent(); ?

? ? ? ? intent.setAction("..."); ?

? ? ? ? Context.sendBroadcast(intent); ?

? ??

? ??

? ? 有序廣播發送方核心代碼:

? ? ? ? Intent intent = new Intent(); ?

? ? ? ? intent.setAction("..."); ?

? ? ? ? Context.sendOrderedBroadcast(intent,null); ?

? ??

? ??

? ? 廣播接收者核心代碼:

? ??

? ? ? ? public class Receiver extends BroadcastReceiver{ ?

? ? ? ? ? ? public void onReceive(Context context, Intent intent) { ?

? ? ? ? ? ? ? ? Bundle bundle = intent.getExtras(); ?

? ? ? ? ? ? ? ? ... ?

? ? ? ? ? ? } ?

? ? ? ? } ?


? ? AndroidManifest.xml

? ? <application> ? ? ? ? ??

? ? ? ? <receiver android:name=".Receiver"> ??

? ? ? ? ? ? <intent-filter android:priority="1000"> ??

? ? ? ? ? ? ? ? <action android:name="com.xiazdong"/> ?

? ? ? ? ? ? </intent-filter> ?

? ? ? ? </receiver> ?

? ? </application> ? ? ? ? ?




三、廣播實例



1.同步廣播實例


? ? (1)廣播發送者:

? ??

? ? package com.xiazdong.broadcastsender; ?

? ??

? ? import android.app.Activity; ?

? ? import android.content.Intent; ?

? ? import android.os.Bundle; ?

? ? import android.view.View; ?

? ? import android.view.View.OnClickListener; ?

? ? import android.widget.Button; ?

? ? import android.widget.Toast; ?

?

? ? public class MainActivity extends Activity { ?

? ? ? ? private Button button; ?

? ? ? ? @Override ?

? ? ? ? public void onCreate(Bundle savedInstanceState) { ?

? ? ? ? ? ? super.onCreate(savedInstanceState); ?

? ? ? ? ? ? setContentView(R.layout.main); ?

? ? ? ? ? ? button = (Button)this.findViewById(R.id.button); ?

? ? ? ? ? ? button.setOnClickListener(listener); ?

? ? ? ? } ?

? ? ? ? private OnClickListener listener = new OnClickListener(){ ?

? ? ? ? ? ? @Override ?

? ? ? ? ? ? public void onClick(View v) { ?

? ? ? ? ? ? ? ? Intent intent = new Intent(); ?

? ? ? ? ? ? ? ? intent.setAction("com.xiazdong"); ?

? ? ? ? ? ? ? ? intent.putExtra("name", "xiazdong"); ?

? ? ? ? ? ? ? ? MainActivity.this.sendBroadcast(intent); ?

? ? ? ? ? ? ? ? Toast.makeText(getApplicationContext(), "發送廣播成功", Toast.LENGTH_SHORT).show(); ?

? ? ? ? ? ? } ?

? ? ? ? }; ?

? ? } ?



(2)廣播接收者

? ? package com.xiazdong.broadcastreceiver1; ?

? ??

? ? import android.content.BroadcastReceiver; ?

? ? import android.content.Context; ?

? ? import android.content.Intent; ?

? ? import android.util.Log; ?

? ??

? ? public class Receiver extends BroadcastReceiver { ?

? ??

? ? ? ? @Override ?

? ? ? ? public void onReceive(Context context, Intent intent) { ?

? ? ? ? ? ? String name = intent.getExtras().getString("name"); ?

? ? ? ? ? ? Log.i("Recevier1", "接收到:"+name); ?

? ? ? ? } ?

? ??

? ? } ?



? ? AndroidManifest.xml

? ??

? ? <receiver android:name=".Receiver"> ?

? ? ? ? <intent-filter> ?

? ? ? ? ? ? <action android:name="com.xiazdong"/> ?

? ? ? ? </intent-filter> ?

? ? </receiver> ?



2.有序廣播實例


? ? 場景說明:

? ??

? ??

? ? (1)廣播發送者

? ??

? ? package com.xiazdong.broadcastsender; ?

? ??

? ? import android.app.Activity; ?

? ? import android.content.Intent; ?

? ? import android.os.Bundle; ?

? ? import android.view.View; ?

? ? import android.view.View.OnClickListener; ?

? ? import android.widget.Button; ?

? ? import android.widget.Toast; ?

?

? ? public class MainActivity extends Activity { ?

? ? ? ? private Button button; ?


? ? ? ? @Override ?

? ? ? ? public void onCreate(Bundle savedInstanceState) { ?

? ? ? ? ? ? super.onCreate(savedInstanceState); ?

? ? ? ? ? ? setContentView(R.layout.main); ?

? ? ? ? ? ? button = (Button)this.findViewById(R.id.button); ?

? ? ? ? ? ? button.setOnClickListener(listener); ?

? ? ? ? } ?

? ? ? ? private OnClickListener listener = new OnClickListener(){ ?

? ? ? ? ? ? @Override ?

? ? ? ? ? ? public void onClick(View v) { ?

? ? ? ? ? ? ? ? Intent intent = new Intent(); ?

? ? ? ? ? ? ? ? intent.setAction("com.xiazdong"); ?

? ? ? ? ? ? ? ? intent.putExtra("name", "xiazdong"); ?

? ? ? ? ? ? ? ? MainActivity.this.sendOrderedBroadcast(intent, null); ? //有序廣播發送 ?

? ? ? ? ? ? ? ? Toast.makeText(getApplicationContext(), "發送廣播成功", Toast.LENGTH_SHORT).show(); ?

? ? ? ? ? ? } ?

? ? ? ? }; ?

? ? } ?



? ? (2)廣播接收者

? ??

? ??

? ? Receiver1

? ? package com.xiazdong.broadcastreceiver1; ?

? ??

? ? import android.content.BroadcastReceiver; ?

? ? import android.content.Context; ?

? ? import android.content.Intent; ?

? ? import android.util.Log; ?

? ??

? ? public class Receiver extends BroadcastReceiver { ?

? ??

? ? ? ? @Override ?

? ? ? ? public void onReceive(Context context, Intent intent) { ?

? ? ? ? ? ? String name = intent.getExtras().getString("name"); ?

? ? ? ? ? ? Log.i("Recevier1", "接收到:"+name); ?

? ? ? ? ? ? abortBroadcast(); ? //Receiver1接收到廣播后中斷廣播 ?

? ? ? ? } ?

? ??

? ? } ?



? ? AndroidManifest.xml

? ??

? ? <receiver android:name=".Receiver"> ?

? ? ? ? <intent-filter android:priority="1000"> <!-- 設置最高優先級 --> ?

? ? ? ? ? ? <action android:name="com.xiazdong"/> ?

? ? ? ? </intent-filter> ?

? ? </receiver> ?



轉載于:https://blog.51cto.com/yym631/1744732

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的广播接收者的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。