導航:首頁 > 操作系統 > androidc發廣播

androidc發廣播

發布時間:2022-07-10 11:30:09

1. 簡述在android中如何發送廣播消息

1.發送廣播
Intent intent = new Intent(BroadcastAction);
Bundle bundle = new Bundle();
bundle.putString("***", SUCCESS);
bundle.putString("FullPathName", mFullPathName);
intent.putExtras(bundle);
sendBroadcast(intent);
2.在Activity中創建一個內部類MyBroadcastReceiver擴展BroadcastReceiver,並在其中實現onReceive方法。
3.在Activity中聲明一個MyBroadcastReceiver類型的成員變數,並注冊:
private MyBroadcastReceiver myBroadcastReceiver;
...
myBroadcastReceiver = new MyBroadcastReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(BroadcastAction);
registerReceiver(receiver, filter);
4.使用完後要記得釋放
unregisterReceiver(receiver);

註:1和2中的 BroadcastAction要是同一個Action

2. Android開發中怎麼把廣播中處理後的信息傳遞給Activity

1

class MyReceiver extends BroadcastReceiver{
/**
* 接收廣播
* @param context 上下文
* @param intent 就是通過sendBroadcast發送的Intent對象
*/
@Override
public void onReceive(Context context, Intent intent) {

}
}

2
//相當於搭電台
//定頻段
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("androidxx.intent.action.CCTV");
//注冊廣播接收器
myReceiver = new MyReceiver();
/**
* 參數一 BroadcastRecriver 對象
* 參數二 IntentFileer對象 此對象用於決定接受什麼頻道的廣播
*/
registerReceiver(myReceiver,intentFilter);

3
//發送廣播信息
Intent intent = new Intent();
intent.setAction("androidxx.intent.action.CCTV");
intent.putExtra("msg","中C");

4
sendBroadcast(intent);

3. Android Studio實現點擊按鈕發送廣播,並動態注冊實現廣播接收者,接收到廣播後顯示一個TOAST

思路:

java">發送廣播
Intentintent=newIntent()
intent.setAction("com.xxx.verify");//這里設置Ation,可以是任意字元串
sendBroadcast(intent);

接收廣播;
//動態注冊廣播接收者
VerifyReceiverreceiver=newVerifyReceiver();
IntentFilterfilter=newIntentFilter();
filter.addAction("com.xxx.verify");//要和發送廣播Action一致
registerReceiver(receiver,filter);
//自定義廣播
{

@Override
publicvoidonReceive(Contextcontext,Intentintent){

//處理業務邏輯,

}

}

4. android廣播機制的廣播機制的三要素

Android廣播機制包含三個基本要素:廣播(Broadcast) - 用於發送廣播;廣播接收器(BroadcastReceiver) - 用於接收廣播;意圖內容(Intent)-用於保存廣播相關信息的媒介。Broadcast是一種廣泛運用的在應用程序之間傳輸信息的機制。而BroadcastReceiver是對發送出來的Broadcast進行過濾接受並響應的一類組件。

5. android中通過什麼方法發送無序廣播

起一個線程,每發一個廣播後就sleep一分鍾,如此循環。(或者接受系統的timechanged這個廣播,這個廣播好像一分鍾發一次)。

Android 在發送廣播時的方法 sendBroadcast(Intent)。

①:Intent myIntent = new Intent();——【創建Intent對象】

②:myIntent.setAction(String)——【設置一般的要執行的動作。參數:動作一個動作的名稱,如ACTION_VIEW。應用程序的具體行動,應與供應商的包名作為前綴。】

③:myIntent.putExtra(String,Object)——【廣播中額外發送的數據,String為自定義key,Object表示多種數據類型】

④:sendBroadcast(myIntent);——【發送廣播】

接收廣播

Android在接收廣播的方法是注冊一個廣播接收器 registerReceiver(MyReceiver,IntentFilter)。

①:首先創建MyReceiver類(類名自定義) 繼承 BroadcastReceiver類。——【創建廣播接收器】

②:在MyReceiver中重寫public void onReceive(Context context, Intent intent)方法。這個方法在接收到廣播後觸發。——【重寫處理方法】

③:在Activity或者Service啟動時 onCreate()、onStartCommand()等方法中實例化 MyReceiver類——【啟動時實例化廣播接收器】

④:IntentFilter filter = new IntentFilter();——【創建IntentFilter對象 意圖過濾器】

⑤:filter.addAction(String);——【在過濾器中加入過濾條件,說明接收什麼廣播】

⑥:registerReceiver(cmdReceiver, filter);——【注冊廣播,參數為(廣播接收器,意圖過濾器)】

6. android發消息和發廣播的區別

2者是都用地發送一則消息。
發消息是一對一的發.主要用於前台起個提示作用,通常有個界面會把消息內容顯示出來。
發廣播是一對多的發.廣播消息發出來後,只有訂閱了該廣播的對象(activity,service等)才會接收廣播出來的消息,並做出相應處理。

7. android 怎麼發送系統廣播

分為4步:

  1. 首先要聲明廣播

  2. 其次要注冊廣播,有兩種方式:xml注冊和代碼注冊

  3. 發送廣播

  4. 收聽開機廣播

    268101305698999

8. android中怎麼發送帶內容的有序廣播

(一),BroadcastReceiver用於監聽被廣播的事件(Intent),為了達到這個目的,BroadcastReceiver必須進行注冊,注冊的方法有以下兩種:
1、靜態注冊:
靜態注冊方式是在AndroidManifest.xml的application裡面定義receiver並設置要接收的action。
如果在清單配置文件中配置了廣播接收器,那麼程序在安裝後會自動注冊廣播接收器。
靜態注冊方式的特點:不管該應用程序是否處於活動狀態,都會進行監聽。
<receiver
android:name=".CallReceiver"
android:enabled="true">
<intent-filter >
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>

其中,MyReceiver為繼承BroadcastReceiver的類,重寫了onReceiver方法,並在onReceiver方法中對廣播進行處理。<intent-filter>標簽設置過濾器,接收指定action廣播。

2、動態注冊:
動態注冊方式是在activity裡面調用當前上下文對象的registerReceiver() 方法 來注冊,和靜態的內容差不多。一個形參是receiver對象,另一個是IntentFilter對象。而IntentFilter構造方法的參數是要接收的action。
動態注冊方式特點:在代碼中進行注冊後,當應用程序關閉後,就不再進行監聽。
MyReceiver receiver = new MyReceiver();
//創建過濾器,並指定action,使之用於接收同action的廣播
IntentFilter filter = new IntentFilter("android.intent.action.PHONE_STATE");
//注冊廣播接收器
registerReceiver(receiver, filter);
(二)、發送廣播:
// 指定廣播目標Action
Intent intent = new Intent("MyReceiver_Action");
// 可通過Intent攜帶消息
intent.putExtra("msg", "發送廣播");
// 發送廣播消息
sendBroadcast(intent);
(三)、注銷BroadcastReceiver:
1、一般在onStart中注冊BroadcastReceiver,在onStop中取消BroadcastReceiver。
2、一個BroadcastReceiver 對象只有在被調用onReceive(Context, Intent)時才有效,當從該函數返回後,該對象就無效的了,結束生命周期。
//注銷廣播接收器
unregisterReceiver(receiver);

9. android c++怎麼發送廣播

最大的可能是廣播沒有注冊

1)第一種不是常駐型廣播,也就是說廣播跟隨activity的生命周期。注意: 在activity結束前,移除廣播接收器。(代碼里注冊)
2)第二種是常駐型,也就是說當應用程序關閉後,如果有信息廣播來,程序也會被系統調用自動運行。(androidmanifest.xml注冊)

閱讀全文

與androidc發廣播相關的資料

熱點內容
噴油螺桿製冷壓縮機 瀏覽:577
python員工信息登記表 瀏覽:375
高中美術pdf 瀏覽:158
java實現排列 瀏覽:511
javavector的用法 瀏覽:979
osi實現加密的三層 瀏覽:230
大眾寶來原廠中控如何安裝app 瀏覽:911
linux內核根文件系統 瀏覽:240
3d的命令面板不見了 瀏覽:523
武漢理工大學伺服器ip地址 瀏覽:146
亞馬遜雲伺服器登錄 瀏覽:521
安卓手機如何進行文件處理 瀏覽:70
mysql執行系統命令 瀏覽:928
php支持curlhttps 瀏覽:142
新預演算法責任 瀏覽:443
伺服器如何處理5萬人同時在線 瀏覽:249
哈夫曼編碼數據壓縮 瀏覽:424
鎖定伺服器是什麼意思 瀏覽:383
場景檢測演算法 瀏覽:616
解壓手機軟體觸屏 瀏覽:348