溫馨提示×

溫馨提示×

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

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

有序廣播怎么在Android應(yīng)用中進(jìn)行發(fā)送

發(fā)布時間:2020-12-05 16:48:09 來源:億速云 閱讀:503 作者:Leah 欄目:移動開發(fā)

有序廣播怎么在Android應(yīng)用中進(jìn)行發(fā)送?相信很多沒有經(jīng)驗(yàn)的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

Android系統(tǒng)提供了兩種廣播類型,一種是有序廣播,一種是有序廣播。

(1)無序廣播是完全異步執(zhí)行,發(fā)送廣播時所有監(jiān)聽這個廣播的廣播接收者都會收到此消息,但接收的順序不確定。

(2)有序廣播是按照接收者的優(yōu)先級接收,只有一個廣播接收者能接收信息,在此廣播接收者中邏輯執(zhí)行完畢后,才會繼續(xù)傳遞。

實(shí)驗(yàn)要求:通過sendOrderedBroadeast()發(fā)送一條有序廣播

1.在activity-main.xml布局文件中代碼如下:

<&#63;xml version="1.0" encoding="utf-8"&#63;> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  xmlns:tools="http://schemas.android.com/tools" 
  android:id="@+id/activity_main" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:paddingBottom="@dimen/activity_vertical_margin" 
  android:background="@drawable/stitch_one" 
  tools:context="cn.edu.bzu.orderedbroadcast.MainActivity"> 
 
  <Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:onClick="send" 
    android:layout_marginTop="50dp" 
    android:text="發(fā)送有序廣播" 
    android:paddingLeft="5dp" 
    android:paddingRight="5dp" 
    android:background="#FFD202" 
    android:textSize="20sp" 
    /> 
</RelativeLayout> 

2.在MainActivity中代碼如下:

package cn.edu.bzu.orderedbroadcast; 
 
import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
 
public class MainActivity extends AppCompatActivity { 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
  } 
  public void send(View view){ 
    Intent intent=new Intent(); 
    //定義廣播事件類型 
    intent.setAction("Intercept_Stitch"); 
    //發(fā)送廣播 
    sendOrderedBroadcast(intent,null); 
  } 
} 

3.創(chuàng)建三個廣播接收者

(1)MyBroadcastReceiverOne

(2)MyBroadcastReceiverTwo

(3)MyBroadcastReceiverThree

(1)在MyBroadcastReceiverOne中代碼如下:

package cn.edu.bzu.orderedbroadcast; 
 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.util.Log; 
 
public class MyBroadcastReceiverOne extends BroadcastReceiver { 
  public MyBroadcastReceiverOne() { 
  } 
 
  @Override 
  public void onReceive(Context context, Intent intent) { 
    Log.i("MyBroadcastReceiverOne","自定義的廣播接收者one,接收到了廣播事件"); 
  } 
} 

(2)在MyBroadcastReceiverTwo中代碼如下:

package cn.edu.bzu.orderedbroadcast; 
 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.util.Log; 
 
public class MyBroadcastReceiverTwo extends BroadcastReceiver { 
  public MyBroadcastReceiverTwo() { 
  } 
 
  @Override 
  public void onReceive(Context context, Intent intent) { 
    Log.i("MyBroadcastReceiverTwo","自定義的廣播接收者Two,接收到了廣播事件"); 
  } 
} 

(3)在MyBroadcastReceiverThree中代碼如下:

package cn.edu.bzu.orderedbroadcast; 
 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.util.Log; 
 
public class MyBroadcastReceiverThree extends BroadcastReceiver { 
  public MyBroadcastReceiverThree() { 
  } 
 
  @Override 
  public void onReceive(Context context, Intent intent) { 
    Log.i("MyBroadcastReceiverThree","自定義的廣播接收者Three,接收到了廣播事件"); 
  } 
} 

4.在配置文件AndroidManifest中代碼如下:

<&#63;xml version="1.0" encoding="utf-8"&#63;> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
  package="cn.edu.bzu.orderedbroadcast"> 
 
  <application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".MainActivity"> 
      <intent-filter> 
        <action android:name="android.intent.action.MAIN" /> 
 
        <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
    </activity> 
 
    <receiver 
    android:name=".MyBroadcastReceiverOne"> 
    <intent-filter android:priority="1000"> 
      <action android:name="Intercept_Stitch"/> 
    </intent-filter> 
    </receiver> 
    <receiver 
      android:name=".MyBroadcastReceiverTwo"> 
      <intent-filter android:priority="200"> 
        <action android:name="Intercept_Stitch"/> 
      </intent-filter> 
    </receiver> 
    <receiver 
      android:name=".MyBroadcastReceiverThree"> 
      <intent-filter android:priority="600"> 
        <action android:name="Intercept_Stitch"/> 
      </intent-filter> 
    </receiver> 
  </application> 
 
</manifest> 

5.實(shí)驗(yàn)效果圖:

有序廣播怎么在Android應(yīng)用中進(jìn)行發(fā)送

運(yùn)行程序后解決如下問題:

問題1:程序啟動后,單擊“發(fā)送有序廣播”按鈕,發(fā)出一條廣播事件,此時觀察LogCat窗口下的提示信息,輸出什么?為什么?

點(diǎn)擊按鈕后出現(xiàn)如下圖所示提示信息,原因是有序廣播根據(jù)優(yōu)先級接收廣播信息的,在配置文件中廣播接收者One的priority值最大,所以它先接收到廣播,其次是Three,最后是Two。其中android:priority="值"是用來設(shè)置廣播接收者的優(yōu)先級的,值越大,優(yōu)先級別越高。

有序廣播怎么在Android應(yīng)用中進(jìn)行發(fā)送

問題2:若將廣播接收者M(jìn)yBroadcastReceiverTwo優(yōu)先級同樣設(shè)置為1000,并將MyBroadcastReceiverTwo注冊在MyBroadcastReceiverOne前面,再來運(yùn)行程序,觀察結(jié)果,你能得出什么結(jié)論?

修改配置文件AndroidManifest中代碼如下:

<receiver 
  android:name=".MyBroadcastReceiverTwo"> 
  <intent-filter android:priority="1000"> 
    <action android:name="Intercept_Stitch"/> 
  </intent-filter> 
</receiver> 
<receiver android:name=".MyBroadcastReceiverOne"> 
  <intent-filter android:priority="1000"> 
    <action android:name="Intercept_Stitch"/> 
</intent-filter> 
</receiver> 
 
<receiver 
  android:name=".MyBroadcastReceiverThree"> 
  <intent-filter android:priority="600"> 
    <action android:name="Intercept_Stitch"/> 
  </intent-filter> 
</receiver> 

運(yùn)行程序出現(xiàn)如下圖所示提示信息,原因是當(dāng)廣播接收者的優(yōu)先級別相同時,先注冊的廣播接收者先接收到廣播。

有序廣播怎么在Android應(yīng)用中進(jìn)行發(fā)送

問題3:修改MyBroadcastReceiverTwo如下,觀察結(jié)果,你又可以得出什么結(jié)論?

public class MyBroadcastReceiverTwo extends BroadcastReceiver { 
  public MyBroadcastReceiverTwo() { 
  } 
 
  @Override 
   public void onReceive(Context context, Intent intent) { 
    Log.i("MyBroadcastReceiverTwo","自定義的廣播接收者Two,接收到了廣播事件"); 
    abortBroadcast();//有序廣播攔截器 
    Log.i("MyBroadcastReceiverTwo","我是廣播接收者Two,廣播被我終止了"); 
  } 
 
} 

運(yùn)行程序出現(xiàn)如下圖所示提示信息,原因是廣播接收者Two的優(yōu)先級最高,所以在Two中寫一個攔截器,優(yōu)先級低的廣播接收者將接收不到廣播信息,所以O(shè)ne和Three沒有接收到廣播事件,也就沒有打印關(guān)于它們的信息。

有序廣播怎么在Android應(yīng)用中進(jìn)行發(fā)送

看完上述內(nèi)容,你們掌握有序廣播怎么在Android應(yīng)用中進(jìn)行發(fā)送的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

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

AI