国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android Intent發送廣播消息實例詳解

2019-12-12 03:07:59
字體:
來源:轉載
供稿:網友

Android Intent發送廣播消息

Intent的另一種用途是發送廣播消息,應用程序和Android系統都可以使用Intent發送廣播消息,廣播消息的內容是可以與應用程序密切相關的數據信息,也可以是Android的系統信息,例如網絡連接變化、電池電量變化、接收的短信或系統設置變化等。如果應用程序注冊了BroadcastReceiver,則可以接受到指定的廣播信息。

使用Intent發送廣播消息非常簡單,只須創建一個Intent,并調用sendBroadcast()函數就可把Intent攜帶的信息廣播出去。但需要注意的是,在構造Intent時必須定義一個全局唯一的字符串,用來標識其要執行的動作,通常使用應用程序包的名稱。如果要在Intent傳遞額外數據,可以用Intent的putExtra()方法。下面的代碼構造了用于廣播消息的Intent,并添加了額外的數據,然后調用sendBroadcast()發送廣播消息:

String UNIQUE_STRING="edu.hrbeu.BroadcastReceiverDemo";Intent intent=new Intent(UNIQUE_STRING);intent.putExtra("key1","value1");intent.putExtra("key2","value2");sendBroadcast(intent);

BroadcastReceiver用于監聽廣播消息,可以在AndroidManifest.xml文件或代碼中注冊一個BroadcastReceiver,并使用Intent過濾器指定要處理的廣播消息。創建BroadcastReceiver須要繼承BroadcastReceiver類,并重載onReceive()方法。示例代碼如下:

public class MyBroadcastReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context,Intent intent){ //TODO: React to the Intent received. }}

當Android系統接收到與注冊BroadcastReceiver匹配的廣播消息時,Android系統會自動調用這個BroadcastReceiver接收廣播消息。在BroadcastReceiver接收到與之匹配的廣播消息后,onReceiver()方法會被調用,但onReceive()方法必須要在5秒鐘內執行完畢,否則Android系統會認為該組件失去響應,并提示用戶強行關閉該組件。

下面為一個簡單示例

發送廣播消息關鍵代碼

botton.setOnClickListener(new OnClickListener()){  public void onClick(View view){   Intent intent=new Intent("edu.hrbeu.BroadcastReceiverDemo");   intent.putExtra("message",entryText.getText().toString());   sendBroadcast(intent);  } }};

在AndroidManifest.xml 文件中注冊 BroadcastReceiver

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="edu.hrbeu.BroadcastReceiverDemo"> <application  android:allowBackup="true"  android:icon="@mipmap/ic_launcher"  android:label="@string/app_name"  android:supportsRtl="true"  android:theme="@style/AppTheme">  <activity   android:name=".BroadcastReceiverDemo"   android:label="@string/app_name"   >   <intent-filter>    <action android:name="android.intent.action.MAIN" />    <category android:name="android.intent.category.LAUNCHER" />   </intent-filter>  </activity>  <receiver android:name=".MyBroadcastReceiver">   <intent-filter>   <action android:name="edu.hrbeu.BroadcastReceiverDemo"/>   </intent-filter>  </receiver> </application></manifest>

在AndroidManifest.xml文件中創建了一個< receiver >節點,其中聲明了Intent過濾器的動作為 edu.hrbeu.BroadcastReceiverDemo,這與發送廣播消息中的Intent的動作一致,表明這個BroadcastReceiver可以接受動作為edu.hrbeu.BroadcastReceiverDemo 的廣播消息。

MyBroadcastReceiver.Java中創建了一個自定義的BroadcastReceiver,其核心代碼如下:

public class MyBroadcastReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context,Intent intent){ String msg=intent.getStringExtra("message"); Toast.makeText(context,msg,Toast.LENGTH_SHORT).show(); }}

代碼第一行首先繼承了BroadcastReceiver類,并在第3行重載了onReveive()函數。當接收到AndroidManifest.xml文件定義的廣播消息后,程序將自動調用onReveive()函數進行消息處理。

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 沙河市| 津南区| 开封市| 伊宁县| 玉环县| 汉阴县| 新源县| 扬中市| 许昌市| 远安县| 华宁县| 南漳县| 蛟河市| 五常市| 通辽市| 石泉县| 海门市| 喀喇| 获嘉县| 铁力市| 马关县| 金坛市| 嵩明县| 英超| 潜山县| 永善县| 乃东县| 涞水县| 长汀县| 武陟县| 阳城县| 娱乐| 上林县| 夏津县| 化德县| 临湘市| 孟村| 大同县| 漳平市| 新巴尔虎左旗| 九龙坡区|