码迷,mamicode.com
首页 > 移动开发 > 详细

android基础---->Broadcast的使用

时间:2016-03-24 01:10:37      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:

  在Android中,Broadcast是一种广泛运用的在应用程序之间传输信息的机制。而BroadcastReceiver是对发送出来的 Broadcast进行过滤接受并响应的一类组件。

 

目录导航:

  1.  广播Broadcast的简要说明
  2.  广播Broadcast的准备工作
  3.  Broadcast广播的测试
  4.  友情链接

 

广播Broadcast的简要说明

一、 广播的种类:标准广播和有序广播

  • 标准广播:是一种完全异步执行的广播,在广播发出之后,所有的广播接收器几乎都会在同一时刻接收到这条广播消息,因此它们之间没有任何先后顺序可言。这种广播的效率会比较高,但同时也意味着它是无法被截断的

   技术分享

  • 有序广播:则是一种同步执行的广播,在广播发出之后,同一时刻只会有一个广播接收器能够收到这条广播消息,当这个广播接收器中的逻辑执行完毕后,广播才会继续传递

  技术分享

二、 注册BroadcastReceiver的两种方式:

  • 静态注册:在AndroidManifest.xml中用标签生命注册,并在标签内用标签设置过滤器
<receiver android:name=".NetworkChangeReceiver">
    <intent-filter>
        <action android:name="com.example.linux.broadcasttest.MyBroadcast" />
    </intent-filter>
</receiver>
  •  动态注册:在java代码中调用registerReceiver(BroadcastReceiver receiver, IntentFilter filter)方法
Intent intent = new Intent();
intent.putExtra("username", "linux");
intent.setAction("com.example.linux.broadcasttest.MyBroadcast");
sendBroadcast(intent);

 

广播Broadcast的准备工作

我们建立一个android项目,结构如下:

技术分享

 

一、 在MainActivity中初始化一些信息:

private final static String TAG = "OtherReceiver";
private IntentFilter intentFilter;
private NetworkChangeReceiver networkChangeReceiver;
private LocalBroadcastManager localBroadcastManager;
private LocalReceiver localReceiver;

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.i(TAG, "main on create");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

 

二、 创建三个广播接收器,用于广播的测试:它们分别如下

  • LocalReceiver本地广播:
package com.example.linux.broadcasttest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

/**
 * Created by Linux on 2016/3/7.
 */
public class LocalReceiver extends BroadcastReceiver {
    private final static String TAG = "OtherReceiver";

    public LocalReceiver() {
        Log.i(TAG, "local receiver constructor.");
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String username = intent.getStringExtra("username");
        Log.i(TAG, "local username: " + username);
    }
}
  • OtherReceiver用于测试有序广播:
package com.example.linux.broadcasttest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class OtherReceiver extends BroadcastReceiver {
    private final static String TAG = "OtherReceiver";
    public OtherReceiver() {
        Log.i(TAG, "other receiver constructor.");
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String username = intent.getStringExtra("username");
        Log.i(TAG, "other username: " + username);
        abortBroadcast();
    }
}
  • NetworkChangeReceiver用于测试系统广播的使用:
package com.example.linux.broadcasttest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

/**
 * Created by Linux on 2016/3/7.
 */
public class NetworkChangeReceiver extends BroadcastReceiver {
    private final static String TAG = "OtherReceiver";

    public NetworkChangeReceiver() {
        Log.i(TAG, "network receiver constructor.");
    }

    @Override
    //Intent是sendBroadcast中的参数
    public void onReceive(Context context, Intent intent) {
        String username = intent.getStringExtra("username");
        Log.i(TAG, "network username: " + username);
    }
}

 

三、 由于广播会涉及到注册,这里我把广播的注册代码贴出来:在AndroidManifest.xml文件中

<receiver android:name=".NetworkChangeReceiver">
    <intent-filter>
        <action android:name="com.example.linux.broadcasttest.MyBroadcast" />
    </intent-filter>
</receiver>
<receiver android:name=".OtherReceiver">
    <intent-filter android:priority="100">
        <action android:name="com.example.linux.broadcasttest.MyBroadcast" />
    </intent-filter>
</receiver>
<receiver android:name=".LocalReceiver">
    <intent-filter>
        <action android:name="com.example.linux.broadcasttest.MyBroadcast" />
    </intent-filter>
</receiver>

 

Broadcast广播的测试

一、 定义方法sendSystemBroad,用于发送系统广播

// 发送系统广播
public void sendSystemBroad(View view) {
    intentFilter = new IntentFilter();
    intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
    networkChangeReceiver = new NetworkChangeReceiver();
    registerReceiver(networkChangeReceiver, intentFilter);
}
  • 打印的结果:
03-23 23:03:20.087 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: other receiver constructor.
03-23 23:03:20.087 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: other username: linux
03-23 23:03:20.097 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: network receiver constructor.
03-23 23:03:20.107 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: network username: linux
03-23 23:03:20.107 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: local receiver constructor.
03-23 23:03:20.107 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: local username: linux

 

二、 定义方法sendBroadCast,用于测试发送标准广播

// 发送标准广播
public void sendBroadCast(View view) {
    Intent intent = new Intent();
    intent.putExtra("username", "linux");
    intent.setAction("com.example.linux.broadcasttest.MyBroadcast");
    sendBroadcast(intent);
}
  • 打印的结果:
03-23 23:05:47.957 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: network receiver constructor.
03-23 23:05:47.967 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: network username: null

 

三、 定义方法sendOrderBroad,用于测试有序广播

// 发送有序广播
public void sendOrderBroad(View view) {
    Intent intent = new Intent();
    intent.putExtra("username", "linux");
    intent.setAction("com.example.linux.broadcasttest.MyBroadcast");
    sendOrderedBroadcast(intent, null);
}
  • 打印的结果:
03-23 23:07:28.877 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: other receiver constructor.
03-23 23:07:28.877 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: other username: linux

 

四、 定义方法sendLocalBroad,用于测试本地广播

// 使用本地广播
public void sendLocalBroad(View view) {
    localBroadcastManager = LocalBroadcastManager.getInstance(this);
    Intent intent = new Intent();
    intent.putExtra("username", "linux");
    intent.setAction("com.example.linux.broadcasttest.MyBroadcast");
    localBroadcastManager.sendBroadcast(intent);
}
  • 打印的结果:
03-23 23:28:09.327 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: local username: linux
03-23 23:28:09.327 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: local username: linux
03-23 23:28:09.327 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: local username: linux

 

友情链接

 

android基础---->Broadcast的使用

标签:

原文地址:http://www.cnblogs.com/huhx/p/baseBroadcast.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!