/**
* 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. FORM Save : ORA-01403 FRM-40735 ORA-06502

    症状: FORM开发后挂上服务器后,运行保存按键提示: ORA-01403: 未找到任何数据 ----------------------------------------------------- ...

  2. hdu4293Groups

    http://acm.hdu.edu.cn/showproblem.php?pid=4293 这题单拉出来写篇吧 确实不错的一题 将每个人说的话 转化一下 可以算出它处在哪个段中 题目就转换成了求不相 ...

  3. API函数

    1. API之网络函数 WNetAddConnection 创建同一个网络资源的永久性连接 WNetAddConnection2 创建同一个网络资源的连接 WNetAddConnection3 创建同 ...

  4. c# post文字图片至服务器

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

  5. 《C#并行编程高级教程》第3章 命令式任务并行 笔记

    Task的使用 var t1 = new Task(() => GenerateAESKeys());var t2 = new Task(() => GenerateMD5Hashes() ...

  6. Prism的IEventAggregator事件聚合器, 事件订阅发布, ViewModel之间的通讯

    WPF中时常会遇到ViewModel之间的通讯,ViewModel并不知道自己的View,但是一个View发生的更改需要通知另外一个View. 举一个例子,软件界面上有个人信息,打开一个界面更改用户的 ...

  7. Enum 枚举

    一: 1. foreach (int val in Enum.GetValues(typeof(AppEnum.HarbourStatus))) { ddlStatus.Items.Add(new L ...

  8. ORM Entities vs. Domain Entities under Entity Framework 6.0

    I stumbled upon the following two articles First and Second in which the author states in summary th ...

  9. 关于 UGUI 字体花屏或乱码。

    我们项目从某个时候开始ui突然开始出现字体花屏现象(unity 开发版本:5.3.6p6),而且很难必现却又时有发生,确实查找和解决起来不太容易. 关于这个问题,uwa官方给出了解释,http://b ...

  10. The Donkey of Gui Zhou

    Problem Description There was no donkey in the province of Gui Zhou, China. A trouble maker shipped ...