欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2017年04月11日 09:58:03   作者:王英豪_  
這篇文章主要介紹了Android Intent發(fā)送廣播消息實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下

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è)全局唯一的字符串,用來標(biāo)識(shí)其要執(zhí)行的動(dòng)作,通常使用應(yī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)聽廣播消息,可以在AndroidManifest.xml文件或代碼中注冊(cè)一個(gè)BroadcastReceiver,并使用Intent過濾器指定要處理的廣播消息。創(chuàng)建BroadcastReceiver須要繼承BroadcastReceiver類,并重載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過濾器的動(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類,并在第3行重載了onReveive()函數(shù)。當(dāng)接收到AndroidManifest.xml文件定義的廣播消息后,程序?qū)⒆詣?dòng)調(diào)用onReveive()函數(shù)進(jìn)行消息處理。

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

相關(guān)文章

最新評(píng)論