Android BroadCast 基础知识

1.Android广播机制概述

Android广播分为两个方面:广播发送者和广播接收者,通常情况下,BroadcastReceiver指的就是广播接收者(广播接收器)。广播作为Android组件间的通信方式,可以使用的场景如下:
1.同一app内部的同一组件内的消息通信(单个或多个线程之间);

2.同一app内部的不同组件之间的消息通信(单个进程);

3.同一app具有多个进程的不同组件之间的消息通信;

4.不同app之间的组件之间消息通信;

5.Android系统在特定情况下与App之间的消息通信。

广播分为静态注册和动态注册:

静态注册:在清代列表中注册

动态注册:在JAVA中注册

 

1.普通广播,

所有监听该广播接受者都可以监听到该广播
 - 同级别接收先后顺序是随机的(无序)
 - 级别低的后收到广播
 - 接收器不能截断广播的继续传播,也不能处理广播
 - 同级别动态注册高于静态注册 <后面例子有出现>

创建广播接收者,new--other-Broadcast Recevier ,之后清代列表中会自动生成一个

<receiver
            android:name=".broadcast.MyReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>    //这个意图过滤器 只接受“com.zx”对应发送的广播
                <action android:name="com.zx" />
            </intent-filter>
        </receiver>
package com.jredu.helloworld.broadcast;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.jredu.helloworld.R;

public class BroadCastActivity extends Activity {
    Button button;
    Button button1;
    MyReceiver receiver;
    IntentFilter intentFilter = new IntentFilter("com.zx");//意图过滤器
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_broad_cast);
        button = (Button) findViewById(R.id.button);
        button1 = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction("com.zx");  //只允许"com.zx"的接收
                intent.putExtra("msg","静态广播");
                sendBroadcast(intent);  //启动发送广播
            }
        });
        receiver = new MyReceiver();           //实例化广播接受者
        registerReceiver(receiver,intentFilter);  //动态注册
        button1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction("com.zx");
                intent.putExtra("msg","动态广播");
                sendBroadcast(intent);
            }
        });

    }

    @Override
    protected void onStop() {
        super.onStop();
        if (receiver!=null){
            unregisterReceiver(receiver); //动态注册的广播必须在Activity关闭前关掉。
        }
   package com.jredu.helloworld.broadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.widget.Toast;

public class MyReceiver extends BroadcastReceiver {
    public MyReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {  //必须重新此方法,生命周期10秒,里面不能添加子线程,如果操作费时耗力的工作,
                                   //
context.startService(new Intent(context,某个服务.class)); 传给服务解决

  String msg
= intent.getStringExtra("msg"); if (TextUtils.isEmpty("msg")){ msg = "hahaha"; } Toast.makeText(context,msg,Toast.LENGTH_SHORT).show(); } }

2.有序广播

按照接收者的优先顺序来接收广播,优先级别在intent-filter中的priority中声明,-10001000之间,值越大优先级越高,可以终止广播的继续传播,接受者可以修改intent的内容。
 - 同级别接收顺序是随机的
 - 级别低的后收到
 - 能截断广播的继续传播,高级别的广播接收器接收广播后能决定时候截断。
 - 能处理广播
 - 同级别动态注册高于静态注册

清单列表中

<receiver
            android:name=".broadcast.OrderBoradCastReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter android:priority="10">  //设置优先级 -1000到1000之间 ,值越大优先级越高
                <action android:name="com.zx" />
            </intent-filter>
        </receiver>
        <receiver
            android:name=".broadcast.OrderBroadCastReceiver1"
            android:enabled="true"
            android:exported="true">
            <intent-filter android:priority="1">
                <action android:name="com.zx"></action>
            </intent-filter>
        </receiver>
package com.jredu.helloworld.broadcast;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.jredu.helloworld.R;

public class OrderBroadCastActivity extends AppCompatActivity {
Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_broad_cast);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction("com.zx");
                intent.putExtra("msg","发送有序广播");
                sendOrderedBroadcast(intent,null); //发送有序广播
            }
        });
    }
}
package com.jredu.helloworld.broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class OrderBoradCastReceiver extends BroadcastReceiver {  //优先级为10的接受者
    public OrderBoradCastReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String msg = intent.getStringExtra("msg");
        Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();
        Log.i("MY",msg);
        Bundle bundle = new Bundle();      
        bundle.putString("newmsg","nidenide");
        setResultExtras(bundle);          //额外传递一个数据
//abortBroadcast(); 中断广播 } }
package com.jredu.helloworld.broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class OrderBroadCastReceiver1 extends BroadcastReceiver { //优先级为1的接受者
    public OrderBroadCastReceiver1() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String msg = intent.getStringExtra("msg");
        Bundle bundle = getResultExtras(true); 
        String s2 = bundle.getString("newmsg");   //拿到数据
        Toast.makeText(context,msg+s2,Toast.LENGTH_SHORT).show();
        Log.i("MY",msg+s2);
    }
}

此时结果toast为,发送有序广播 发送有序广播nidenide  发送有序广播 ,为什么出现这种效果呢?因为有序广播优先级高于普通广播,广播接收者都喜好"com.zx",所以前两个是有序的广播,后一个出现是普通广播接收的。

 

 

<receiver
android:name=".broadcast.MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.zx" />
</intent-filter>
</receiver>
posted @ 2016-10-09 09:13  早上~得~喝粥  阅读(494)  评论(0编辑  收藏  举报