[android开发入门]初识BroadcastReceiver
来源:互联网 发布:ftp需要开放的端口 编辑:程序博客网 时间:2024/06/13 16:57
作者:sundroid
个人站点:sundroid.cn 邮箱: hfutsnjc@163.com 微博:http://weibo.com/Sundroid
Android系统引入了一种信息传播机制-BroadcastReceiver,这是一种广泛应用于程序之间传输信息的方式,在系统运行过程中会产生很多的系统通知,比如地域变换、电量不足,BroadcastReceiver就相当于收音机,而许许多多的广播台就是消息的发送者,它们通过特定的频率(Action相同),来接受和发送内容。
在Android系统中发送和接收工作分别由sendBroadcast(Intent intent)方法和注册的BroadcastReceiver类来完成,并且只有发送sendBroadcast(Intent intent)和在manifests中
BroadcastReceiver注册的action一致,才可以正确接收信息。
MainActivity
package cn.sundroid.broadcast;import android.app.Activity;import android.content.Intent;import android.support.v7.app.ActionBarActivity;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 Button send; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); send = (Button) findViewById(R.id.send); send.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); //设置广播内容 intent.putExtra("CONTENT","test broadcast"); //设置广播的action intent.setAction("cn.sundroid.broadcast"); //发送广播 sendBroadcast(intent); } }); }}
Receiver
package cn.sundroid.broadcast;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class Receiver extends BroadcastReceiver { /** * * @param context * @param intent */ @Override public void onReceive(Context context, Intent intent) { Log.i("RECEIVER",intent.getStringExtra("CONTENT")); }}
package cn.sundroid.broadcast;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class OtherReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO: This method is called when the BroadcastReceiver is receiving Log.i("OTHERRECEIVER", intent.getStringExtra("CONTENT")); }}
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.sundroid.broadcast" > <application android:allowBackup="true" android:icon="@mipmap/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=".Receiver" android:enabled="true" android:exported="true" > <intent-filter> <action android:name="cn.sundroid.broadcast" /> </intent-filter> </receiver> <receiver android:name=".OtherReceiver" android:enabled="true" android:exported="true" > <intent-filter> <action android:name="cn.sundroid.receiver" /> </intent-filter> </receiver> </application></manifest>
我们可以看出结果中打印的Log为"RECEIVER"+test broadcast,这里的调用的BroadcastReceiver为Receiver,而不是OtherReceiver,就是因为OtherReceiver在manifests里面注册的anction和sendBroadcast(Intent intent)里面设置的Action不一致的结果,那么如果Receiver和OtherReceiver在manifests里都设置相同的action,结果会如何呢?
这里就不必再做解释了吧!
后台服务还可以查看初识Service
- [android开发入门]初识BroadcastReceiver
- [Android开发入门]初识Service
- Android开发BroadcastReceiver详解
- Android开发之BroadcastReceiver
- BroadcastReceiver初识:
- 初识BroadcastReceiver
- Android入门笔记之BroadcastReceiver
- Android入门--BroadcastReceiver的使用
- Android开发 --- 广播初步 BroadcastReceiver
- Android开发--BroadcastReceiver介绍1
- Android开发之BroadcastReceiver详解
- 了解Android开发利器:BroadcastReceiver
- android开发 - BroadcastReceiver 广播接收者
- android开发 - BroadcastReceiver 广播接收者
- Android开发之BroadcastReceiver组件
- Android开发之BroadcastReceiver详解
- Android开发之BroadcastReceiver详解
- Android开发之BroadcastReceiver详解
- 研发与技术人员绩效管理专家胡立
- 移动仿真:网络带宽控制
- UVA 10534 - Wavio Sequence(DP)
- VM Virtualbox中安装完Ubuntu后重启异常问题
- 项目绩效管理与考核专家胡立
- [android开发入门]初识BroadcastReceiver
- turtlebot(hydro)学习笔记一:两台PC机间通信
- 设计模式概述
- Google Chrome开发者工具-移动仿真:触摸事件仿真
- Nginx二次开发(1.3)Nginx数据类型
- 流程绩效管理与考核专家胡立
- JSON简单介绍
- 目标与绩效管理实战专家胡立
- [FastReport]关系(Relation)的使用1