Android_Broadcast
/**
* 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的更多相关文章
随机推荐
- 【POJ】2823 Sliding Window
单调队列. /* 2823 */ #include <iostream> #include <sstream> #include <string> #include ...
- 在Sublime Text 3中配置编译和运行C++程序
下载解压MinGW至目标目录,本次安装的解压目录为C:\MinGW 设置环境变量.右击我的电脑,属性-->高级-->环境变量. 在系统环境变量PATH里添加C:\MinGW\bin(如果里 ...
- Deep Learning Overview
[Ref: http://en.wikipedia.org/wiki/Deep_learning] Definition: a branch of machine learning based on ...
- (转)如何正确使用C++多重继承
http://bigasp.com/archives/486 如何正确使用C++多重继承 2011年06月17日 — Asp J 原创文章,转载请注明:转载自Soul Apogee本文链接地址:如何正 ...
- M-矩阵
实方阵 $A$ 称为 $M$-矩阵, 是指 $A=cI-B$, $B\geq 0$, $c\geq \rho(B)$. 这里, $M$ 据说是暗指 Minkowski.
- 《C程序设计语言现代方法》第5章 编程题
1 编写一个程序,确定一个数的位数. #include <stdio.h> int main() { ; ) { cnt++; } printf("%d\n", cnt ...
- 给Sublime Text 2安装CTags插件
以Windows操作系统为例介绍安装过程: 安装ctags应用程序. 到CTags的官方网站下载最新版本,解压后将ctags.exe文件放到系统的搜索路径中. 安装Sublime Text 2的Pac ...
- 【解决】hbase regionserver意外关机启动失败 [main] mortbay.log: tmpdir java.io.IOException: Permission denied
错误信息: 015-12-24 10:57:26,527 INFO [main] mortbay.log: jetty-6.1.26.cloudera.4 2015-12-24 10:57:26,5 ...
- HW4.41
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- POJ1276 - Cash Machine(多重背包)
题目大意 给定一个容量为M的背包以及n种物品,每种物品有一个体积和数量,要求你用这些物品尽量的装满背包 题解 就是多重背包~~~~用二进制优化了一下,就是把每种物品的数量cnt拆成由几个数组成,1,2 ...