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

首頁(yè) > 系統(tǒng) > Android > 正文

Android Intent發(fā)送廣播消息實(shí)例詳解

2019-10-23 18:31:45
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

Android Intent發(fā)送廣播消息

Intent的另一種用途是發(fā)送廣播消息,應(yīng)用程序和Android系統(tǒng)都可以使用Intent發(fā)送廣播消息,廣播消息的內(nèi)容是可以與應(yīng)用程序密切相關(guān)的數(shù)據(jù)信息,也可以是Android的系統(tǒng)信息,例如網(wǎng)絡(luò)連接變化、電池電量變化、接收的短信或系統(tǒng)設(shè)置變化等。如果應(yīng)用程序注冊(cè)了BroadcastReceiver,則可以接受到指定的廣播信息。

使用Intent發(fā)送廣播消息非常簡(jiǎn)單,只須創(chuàng)建一個(gè)Intent,并調(diào)用sendBroadcast()函數(shù)就可把Intent攜帶的信息廣播出去。但需要注意的是,在構(gòu)造Intent時(shí)必須定義一個(gè)全局唯一的字符串,用來(lái)標(biāo)識(shí)其要執(zhí)行的動(dòng)作,通常使用應(yīng)用程序包的名稱(chēng)。如果要在Intent傳遞額外數(shù)據(jù),可以用Intent的putExtra()方法。下面的代碼構(gòu)造了用于廣播消息的Intent,并添加了額外的數(shù)據(jù),然后調(diào)用sendBroadcast()發(fā)送廣播消息:

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

BroadcastReceiver用于監(jiān)聽(tīng)廣播消息,可以在AndroidManifest.xml文件或代碼中注冊(cè)一個(gè)BroadcastReceiver,并使用Intent過(guò)濾器指定要處理的廣播消息。創(chuàng)建BroadcastReceiver須要繼承BroadcastReceiver類(lèi),并重載onReceive()方法。示例代碼如下:

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

當(dāng)Android系統(tǒng)接收到與注冊(cè)BroadcastReceiver匹配的廣播消息時(shí),Android系統(tǒng)會(huì)自動(dòng)調(diào)用這個(gè)BroadcastReceiver接收廣播消息。在BroadcastReceiver接收到與之匹配的廣播消息后,onReceiver()方法會(huì)被調(diào)用,但onReceive()方法必須要在5秒鐘內(nèi)執(zhí)行完畢,否則Android系統(tǒng)會(huì)認(rèn)為該組件失去響應(yīng),并提示用戶強(qiáng)行關(guān)閉該組件。

下面為一個(gè)簡(jiǎn)單示例

發(fā)送廣播消息關(guān)鍵代碼

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 文件中注冊(cè) 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文件中創(chuàng)建了一個(gè)< receiver >節(jié)點(diǎn),其中聲明了Intent過(guò)濾器的動(dòng)作為 edu.hrbeu.BroadcastReceiverDemo,這與發(fā)送廣播消息中的Intent的動(dòng)作一致,表明這個(gè)BroadcastReceiver可以接受動(dòng)作為edu.hrbeu.BroadcastReceiverDemo 的廣播消息。

MyBroadcastReceiver.Java中創(chuàng)建了一個(gè)自定義的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類(lèi),并在第3行重載了onReveive()函數(shù)。當(dāng)接收到AndroidManifest.xml文件定義的廣播消息后,程序?qū)⒆詣?dòng)調(diào)用onReveive()函數(shù)進(jìn)行消息處理。

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


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到Android開(kāi)發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 新营市| 从化市| 井冈山市| 咸阳市| 武夷山市| 巩义市| 昌宁县| 双鸭山市| 晋州市| 云和县| 开封县| 海丰县| 邵东县| 襄城县| 封丘县| 邹城市| 遂溪县| 双辽市| 台前县| 阿合奇县| 姚安县| 建始县| 迁西县| 奉新县| 马尔康县| 庆安县| 昌乐县| 泗水县| 乌兰察布市| 元阳县| 探索| 九龙坡区| 息烽县| 凤阳县| 西丰县| 万山特区| 江山市| 通城县| 喀什市| 华池县| 民权县|