溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android - Intent廣播(broadcast)

發(fā)布時間:2020-06-29 05:05:08 來源:網(wǎng)絡 閱讀:513 作者:morndragon 欄目:移動開發(fā)

Intent廣播(broadcast)


本文地址: http://blog.csdn.net/caroline_wendy/article/details/21590339


Intent可以使用sendBroadcast(), 在應用組件之間廣播事件;

使用BroadReceiver類監(jiān)聽和響應Broadcast Intent, 需要注冊(register)過濾器(intent-filter);


1. 創(chuàng)建Broadcast Intent

構建需要廣播的Intent, 包含Intent標識字符串, 和數(shù)據(jù)(data, extras), 使用sendBroadcast()發(fā)送;

Intent標識字符串, 可以標識要廣播的字符串, 應該是能夠標志的唯一字符串,

public static final String [ACTION_STRING] = "[mzx].[clwang].action.[ACTION_STRING]";

Intent包含數(shù)據(jù), 進行廣播, 可以使用data屬性指定一個URI, 可以在extras添加額外的基本值;

Intent intent = new Intent("[mzx].[clwang].action.[ACTION_STRING");

sendBroadcast(intent);


2. 接收Broadcast Intent

使用Broadcast Receiver來監(jiān)聽Broadcast Intent, 在代碼或manifest中注冊, 使用Intent FIlter表明監(jiān)聽的Intent(標識符)和數(shù)據(jù);

應用程序可以被Intent啟動, 可以創(chuàng)建事件驅動的應用程序;

Broadcast Receiver, 繼承BroadcastReceiver類, 重寫onReceive事件, 進行處理, 必須在5秒之內(nèi)完成;

代碼注冊:

注冊BroadcastReceiver, 需要使用IntentFilter, 指明標識符; 使用BroadReceiver類, 指明接收器;

注冊: registerReceiver(receiver, filter); 注銷:unregisterReceiver(receiver);

Manifest注冊:

<receiver android:name = ".[Receiver]">

    <intent-filter>

        <action android:name="[mzx].[clwang].action.[ACTION_STRING">

    </intent-filter>

</receiver>



Android - Intent廣播(broadcast)

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI