/**
* Broadcast(广播):是一种广泛运用的在应用程序之间传输信息的机制
*
* BroadcastReceiver(广播接收者)
* 是对发送出来的广播进行过滤接收并响应的一类组件,它就是用来接收来自系统和应用中的广播
* 使用方法:
* 发送:把信息装入一个intent对象(action,category)
* 通过调用相应方法将intent对象以广播形式发送出去
* sendBroadcast()
* sendOrdercast()
* sendStickyBroadcast()
* 注意!!!
* BroadcastReceiver生命周期只有十秒左右,
* 如果在onReceiver内做超过十秒内的事情,就会报错
* 应该通过发送Intent给service,由service来完成
* 不能用子线程
*
*广播的种类:
* 普通广播,有序广播,异步广播(不能讲处理结果传给下一个接收者,无法终止广播)
* 普通广播的特点:
* -同级别接收先后是随机的(无序)
* -级别低的后收到广播(priority)
* -接收器不能截断广播的传播也不能处理广播,否则报错
* -同级别动态注册高于静态注册
*
* 有序广播的特点:
* -同级别接收顺序是随机的
* -能截断广播的继续传播,高级别的广播接收器收到该广播后,可以决定把该广播是否截断
* -接收器能截断广播的继续传播,也能处理广播
* -同级别动态注册高于静态注册
*
*静态注册:在manifest里面注册(与activity类似)
* <receiver android:name="com.examle.android_broadcast.MyBroadcastReceiver1">
<intent-filter android:priority="200" >
<action android:name="broadcast_one"/>
</intent-filter>
</receiver>
android:priority="200" 优先级,范围-1000到1000
动态注册:在MainActivity中注册,
IntentFilter filter = new IntentFilter("broadcast_one");//创建过滤器,过滤动作为broadcast_one的广播
MyBroadcastReceiver1 receiver = new MyBroadcastReceiver1();//创建广播接收对象
registerReceiver(receiver, filter);//注册广播
!!!注意:动态注册广播后需要在广播接收后关闭广播(重写onDestory方法)
* @author Administrator
*
*/

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android_broadcast"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.BROADCAST_STICKY"/> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
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="com.example.android_broadcast.MyBroadcastReceiver1">
<intent-filter android:priority="200">
<action android:name="broadcast_one"/>
</intent-filter>
</receiver>
<receiver android:name="com.example.android_broadcast.MyBroadcastReceiver2">
<intent-filter android:priority="100">
<action android:name="broadcast_one"/>
</intent-filter>
</receiver>
</application> </manifest>

xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.android_broadcast.MainActivity" >
<Button
android:id="@+id/btn_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="doClick"
android:text="发送一条普通广播"/>
<Button
android:id="@+id/btn_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="doClick"
android:text="发送一条有序广播"/>
<Button
android:id="@+id/btn_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="doClick"
android:text="发送一条异步广播"/> </LinearLayout>

源代码MainActivity:

package com.example.android_broadcast;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button; public class MainActivity extends Activity { private MyBroadcastReceiver3 receiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*IntentFilter filter = new IntentFilter("broadcast_one");//创建过滤器,过滤动作为broadcast_one的广播
MyBroadcastReceiver1 receiver = new MyBroadcastReceiver1();//创建广播接收对象
registerReceiver(receiver, filter);//注册广播
*/ }
public void doClick(View view){
switch(view.getId()){
case R.id.btn_1://发送一条普通广播
Intent intent = new Intent();
intent.putExtra("msg", "这是一条普通广播");//设置发送的信息
intent.setAction("broadcast_one");//设置发送的动作
sendBroadcast(intent);//发送广播 break;
case R.id.btn_2://发送一条有序广播
Intent intent2 = new Intent();
intent2.putExtra("msg", "这是一条有序广播");
intent2.setAction("broadcast_one");//跳转的动作
sendOrderedBroadcast(intent2, null);
break;
case R.id.btn_3://发送一条异步广播,
//异步广播不能终止也就是会每次都存在,用先发送后注册来说明,点击n次出现n条广播 unregisterReceiver(receiver);该方法对其并不起作用
Intent intent3 = new Intent();
intent3.putExtra("msg", "这是一条异步广播");
intent3.setAction("broadcast_three");
sendStickyBroadcast(intent3);
IntentFilter filter = new IntentFilter("broadcast_three");
receiver = new MyBroadcastReceiver3();
registerReceiver(receiver, filter); break; }
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
//unregisterReceiver(receiver);
}
}

MyBroadcastReceiver1:

package com.example.android_broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle; public class MyBroadcastReceiver1 extends BroadcastReceiver{ @Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String s = intent.getStringExtra("msg");
System.out.println("broadcast_one接收消息:"+s);
//有序广播中,可以对收到的数据进行截断或处理
//abortBroadcast();//截断广播,如果是普通广播,就会失败
Bundle bundle = new Bundle();
bundle.putString("newMsg", "数据被广播1处理过-->"+s);
setResultExtras(bundle);
} }

MyBroadcastReceiver2:

package com.example.android_broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle; public class MyBroadcastReceiver2 extends BroadcastReceiver{ @Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String s = intent.getStringExtra("msg");
System.out.println("broadcast_two接收消息:"+s);
Bundle bundle = getResultExtras(true);
String s2 = bundle.getString("newMsg");
System.out.println("broadcastreceiver2得到的结果是:"+s2);
} }

MyBroadcastReceiver3:

package com.example.android_broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent; public class MyBroadcastReceiver3 extends BroadcastReceiver{ @Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String s = intent.getStringExtra("msg");
System.out.println("broadcast_three接收消息:"+s);
//abortBroadcast();//截断广播,因为是普通广播,所以会失败 } }

Android_Broadcast的更多相关文章

随机推荐

  1. win2003+IIS6+PHP5.3.8+MSSQL2008的安装配置

  2. [ECNU 1624] 求交集多边形面积

    求交集多边形面积 Time Limit:1000MS Memory Limit:30000KB Total Submit:98 Accepted:42 Description 在平面上有两给定的凸多边 ...

  3. 《C#并行编程高级教程》第5章 协调数据结构 笔记

    本章介绍了一些轻量级的同步原语,其中有很大部分是.NET Framework 4才引入的. System.Threading.Barrier 用于一段程序分成多个阶段,每个阶段的开始都需要之前的阶段完 ...

  4. Chrome已原生支持“Chrome To Mobile”

    完成PC和手机端Chrome的同gmail帐号绑定后,即可按如下操作进行: 已知在版本“19.0.1084.15”中,这个功能默认未开启,需要进入“chrome://flags/”进行手工启用(早几期 ...

  5. [转]关于WM_NCHITTEST消息

    http://www.cnblogs.com/GnagWang/archive/2010/09/12/1824394.html 我为了移动一个无标题栏的窗体,使用了WM_NCHITTEST消息,这个消 ...

  6. EMC

    1.EMC的概念 EMC(Electro Magnetic Compatibility)即电磁兼容,是指设备或系统在其电磁环境中符合要求运行并不对其环境中的任何设备产生无法忍受的电磁干扰的能力.就是它 ...

  7. linux驱动程序之电源管理 之linux休眠与唤醒(2)

    在Linux中,休眠主要分三个主要的步骤:(1)冻结用户态进程和内核态任务:(2)调用注册的设备的suspend的回调函数:(3)按照注册顺序休眠核心设备和使CPU进入休眠态.       冻结进程是 ...

  8. oracle 日志学习(转载)

    一,重做日志概念 重做日志文件(redo log file)对于Oracle数据库至关重要.它们是数据库的事务日志.通常只用于恢复,不过也可以用于以下工作: q 系统崩溃后的实例恢复 q 通过备份恢复 ...

  9. ssh,telnet远程AIX以及数据传输问题

    (1)telnet远程 输入:telnet 目标IP 输入用户名,密码 进入远程机器 (2)AIX默认安装ftp,使用ftp传输数据 ftp 目标IP地址 输入用户名,密码(远程服务器的) 或者输入f ...

  10. Clean Code – Chapter 4: Comments

    “Don’t comment bad code—rewrite it.”——Brian W.Kernighan and P.J.Plaugher The proper use of comments ...