/**
* 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. c# post文字图片至服务器

    最近由于项目需要实现c#提交文字及数据至服务器,因此研究了一下c# php数据传送: 下面用一个示例来演示,c# post文字+图片 ,php端接收: post提交数据核心代码(post数据提交) ? ...

  2. Xcode7创建纯代码空白工程

    0: Create a new project  with  'single view controller' A: Xcode Settings 1: migrate launch image B: ...

  3. Log4Net 配置和使用

    转:http://www.cnblogs.com/chencidi/archive/2010/01/12/1645291.html web.config配置如下: <?xml version=& ...

  4. BILL.WEI]stimulsoft reports 中panel 妙用

    我们在通过stimulsoft reports做报表,有的时候,我们需要通过合并报表的一些中间列元素,我们就可以用到panel组件 如下图,我们需要合并报表中间项,一般手段达不到要求,只能通过嵌套pa ...

  5. 2DPlatformer-SLua 编辑器 UI 美化

    在我的开源项目 2DPlatformer-SLua 中,YwLuaMonoBehaviour 是非常重要的一个组件,它负责从 MonoBehaviour 中直接驱动内建的事件到 Lua 中的类和逻辑, ...

  6. 【原】SparkContex源码解读(一)

    版权声明:本文为原创文章,未经允许不得转载. SparkContext(简称sc)是Spark程序的主入口,代表一个连接到Spark集群(Standalone.YARN.Mesos三种集群部署模式)的 ...

  7. linux中grep和egrep的用法

    1. grep简介 grep (global search regular expression_r(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大 ...

  8. Java 中 String 类和StringBuilder 类的常用方法

    String 类提供了许多用来处理字符串的方法,例如,获取字符串长度.对字符串进行截取.将字符串转换为大写或小写.字符串分割等,下面我们就来领略它的强大之处吧. String 类的常用方法: 结合代码 ...

  9. IOS开发UIImage中stretchableImageWithLeftCapWidth方法的解释

    - (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCap ...

  10. Android 各种MIME类型和文件类型的匹配表

    MIME:全称Multipurpose Internet Mail Extensions,多功能Internet 邮件扩充服务.它是一种多用途网际邮件扩充协议,在1992年最早应用于电子邮件系统,但后 ...