当前位置:   article > 正文

Android有序广播OrderedBroadcast_orderedbroadcastintent

orderedbroadcastintent

Broadcast被分成两种:

1.Normal Broastcast(普通广播)

    Normal Broadcast是完全异步的,可以在同一时刻(逻辑上)被所有接收者收到,消息传递的效率比较高。但缺点是接收者不能将处理结果传递给下一个接收者,并且无法终止Broadcast Intent的传播。

 

2.Ordered Broadcast(有序广播)

    Ordered Broadcast的接收者将按预先声明的优先级依次接收Broadcast。如:A的级别高于BB的级别高于C。那么,Broadcast先传给A,再传给B,最后传给C。优先级声明在<intent-filter.../>元素的android:priority属性中,数越大优先级别越高,取值范围为-1000~1000,优先级也可以调用IntentFilter对象的setPriority()进行设置。Ordered Broadcast接收者可以终止Broadcast Intent的传播,Broadcast Intent的传播一旦终止,后面的接收者就无法接收到Broadcast。另外,Ordered Broadcast的接收者可以将数据传递给下一个接收者,如:A得到Broadcast后,可以往它的结果对象中存入数据,当Broadcast传给B时,如:A得到Broadcast后,可以往它的结果对象中存入数据,当Broadcast传给B时,B可以从A的结果对象中得到A存入的数据。

 

    Context提供的如下两个方法用于发送广播。

    1.sendBroadcast():发送Normal Broadcast

    2.sendOrderedBroadcast():发送Ordered Broadcast

 

    对于Ordered Broadcast而言,系统会根据接收者声明的优先级按顺序逐个执行接收,优先接收到Broadcast的接收者可以终止Broadcast,调用BroadcastReceiverabortBroadcast()方法即可终止Broadcast。如果Broadcast被前面的接收者终止,后面的接收者就再也无法接收到Broadcast了。

 不仅如此,对于Ordered来说,优先接收到Broadcast的接收者可以通过setResultExtras(Bundle)方法将处理结果存入Broadcast中,然后传给下一个接收者,下一个接收者通过代码:Bundle bundle = getResultExtras(true)可以获取上一个接收者存入的数据。


/********************************************************/


下面是一个实例:

工程结构:



全部代码:

AndroidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.example.leidong.sortedbroadcast">
  4. <application
  5. android:allowBackup="true"
  6. android:icon="@mipmap/ic_launcher"
  7. android:label="@string/app_name"
  8. android:supportsRtl="true"
  9. android:theme="@style/AppTheme">
  10. <activity android:name=".MainActivity">
  11. <intent-filter>
  12. <action android:name="android.intent.action.MAIN" />
  13. <category android:name="android.intent.category.LAUNCHER" />
  14. </intent-filter>
  15. </activity>
  16. <receiver android:name=".MyReceiver1">
  17. <intent-filter android:priority="1">
  18. <action android:name="com.example.leidong.action.OrderedBroadcast"/>
  19. </intent-filter>
  20. </receiver>
  21. <receiver android:name=".MyReceiver2">
  22. <intent-filter android:priority="0">
  23. <action android:name="com.example.leidong.action.OrderedBroadcast"/>
  24. </intent-filter>
  25. </receiver>
  26. </application>
  27. </manifest>

main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical"
  7. android:background="#2b2b2b">
  8. <Button
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:layout_marginLeft="5pt"
  12. android:layout_marginRight="5pt"
  13. android:layout_marginTop="100pt"
  14. android:text="S E N D"
  15. android:textColor="#ffffff"
  16. android:textSize="10pt"
  17. android:textStyle="bold"
  18. android:background="#696969"
  19. android:id="@+id/sendButton"
  20. android:layout_gravity="center_horizontal" />
  21. </LinearLayout>

MainActivity.java

  1. package com.example.leidong.sortedbroadcast;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.view.View;
  6. import android.widget.Button;
  7. /**
  8. * Created by leidong on 2016/9/6.
  9. */
  10. public class MainActivity extends AppCompatActivity {
  11. Button sendButton;
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.main);
  16. sendButton = (Button)findViewById(R.id.sendButton);
  17. sendButton.setOnClickListener(new View.OnClickListener() {
  18. @Override
  19. public void onClick(View view) {
  20. Intent intent = new Intent();
  21. intent.setAction("com.example.leidong.action.OrderedBroadcast");
  22. intent.putExtra("msg","HaHaHaHaHa!!!");
  23. sendOrderedBroadcast(intent, null);
  24. }
  25. });
  26. }
  27. }

MyReceiver1.java

  1. package com.example.leidong.sortedbroadcast;
  2. import android.content.BroadcastReceiver;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.widget.Toast;
  7. /**
  8. * Created by leidong on 2016/9/6.
  9. */
  10. public class MyReceiver1 extends BroadcastReceiver {
  11. @Override
  12. public void onReceive(Context context, Intent intent) {
  13. Toast.makeText(context,
  14. "接收到的Intent的Action为:" + intent.getAction() + "\n 消息内容是:" + intent.getStringExtra("msg"),
  15. Toast.LENGTH_LONG).show();
  16. //创建一个Bundle对象并存入数据
  17. Bundle bundle = new Bundle();
  18. bundle.putString("first", "LaLaLaLa!!!");
  19. //将Bundle放入结果中
  20. setResultExtras(bundle);
  21. }
  22. }


MyReceiver2.java

  1. package com.example.leidong.sortedbroadcast;
  2. import android.content.BroadcastReceiver;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.widget.Toast;
  7. /**
  8. * Created by leidong on 2016/9/6.
  9. */
  10. public class MyReceiver2 extends BroadcastReceiver {
  11. @Override
  12. public void onReceive(Context context, Intent intent) {
  13. Bundle bundle = getResultExtras(true);
  14. //解析前一个BroadcastReceiver所存入的key为first的消息
  15. String first = bundle.getString("first");
  16. System.out.println(first);
  17. Toast.makeText(context,
  18. "我是第二个BroadcastReceiver,我收到的消息是:" + first,
  19. Toast.LENGTH_LONG).show();
  20. }
  21. }
/************************************************************/

 AVD运行:

点击SEND按钮后:


等待几秒后:


    

 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号