Android学习之BroadcastReceiver
来源:互联网 发布:mac系统怎么远程控制 编辑:程序博客网 时间:2024/05/16 17:08
BroadcastReceiver是Android系统引入的一种消息广播机制,它广泛应用于应用程序之间传输信息。通常,在系统运行过程中,后台会发起大量的广播通知,如地域变换、电量不足、来电来短信来应用消息提醒等。
BroadcastReceiver就相当于收音机,而各种各样的广播电台就是消息的发送者。电台通过特定的频率发送内容,收音机只有将接受频率调成和广播电台一样的频率便可以收听相应电台的内容了。
在Android系统中,发送和接受的工作分别由sendBroadcast方法和注册的BroadcastReceiver类完成。并且只有当发送和接受具有相同频率时,接受者才能收到发送者发出的消息,这里的频率就是Intenr对象的action属性。
示例:
单击按钮时,发送广播,注册两个广播接收器(创建BroadcastReceiver类的两个对象,并在AndroidManifest.xml中注册)。当接收器与广播的action相匹配时,则会执行该接收器中的onReceive方法,在控制台打印广播信息。
一共三个java文件:MainActivity.java, InValidateReceiver.java, ValidateReceiver.java
1. MainActivity.java ( src/com.example.broadcast.MainActivity.java)
package com.example.broadcast;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity { private Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = (Button)findViewById(R.id.btn); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { //1.create new intent. Intent intent = new Intent(); //2.set broadcast content. intent.putExtra("CONTENT", "This is broadcast demo."); //3.set broadcast action intent.setAction("com.example.broadcast.validate"); //4.send broadcast sendBroadcast(intent); } }); }}
2. InValidateReceiver.java ( src/com.example.broadcast.InValidateReceiver.java)
package com.example.broadcast;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class InValidateReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { Log.v("InValidateReceiver", intent.getStringExtra("CONTENT")); }}
3. ValidateReceiver.java ( src/com.example.broadcast.ValidateReceiver.java)
package com.example.broadcast;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class ValidateReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { Log.v("ValidateReceiver", intent.getStringExtra("CONTENT")); }}
4. activity_main.xml -->MainActivity的布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button //创建一个按钮 android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="btn" /></LinearLayout>
5. AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.broadcast" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="21" /> <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/>注册BroadcastReceiver <receiver android:name=".ValidateReceiver" > <intent-filter > <action android:name="com.example.broadcast.validate"/> </intent-filter> </receiver> <receiver android:name=".InValidateReceiver" > <intent-filter > <action android:name="com.example.broadcast.invalidate"/> </intent-filter> </receiver> </application></manifest>
从结构上可知接收器只接收与自己有相同的action的广播,所以这里ValidateReceiver接收广播信息,而不是InValidateReceive。
1 0
- android学习之-BroadcastReceiver
- Android学习之BroadcastReceiver
- android学习笔记之BroadcastReceiver
- Android学习之BroadcastReceiver总结
- Android学习之BroadcastReceiver总结
- Android之BroadcastReceiver基础学习
- Android学习笔记之BroadcastReceiver
- android基础学习之BroadcastReceiver
- Android学习笔记之BroadcastReceiver
- android学习笔记之六广播(BroadcastReceiver)
- android学习笔记之六广播(BroadcastReceiver)
- Android开发之短信拦截--BroadcastReceiver学习
- android学习笔记之BroadcastReceiver应用详解
- Android学习笔记--四大组件之BroadcastReceiver
- Android应用开发学习笔记之BroadcastReceiver
- Android学习之BroadCastReceiver的机制
- Android学习笔记之二十 BroadcastReceiver
- (转载)Android学习之旅- BroadCastReceiver
- VC++获取当前系统时间日期 GetSystemTime()函数的用法
- More Effective C++ 第三部分 异常
- 从innodb中恢复数据
- 【凯子哥带你夯实应用层】新手必备的常用代码片段整理(一)
- Valid Sudoku
- Android学习之BroadcastReceiver
- mysql导入sql文件
- leetcode -- Gray Code -- 数学题
- PowerDesigner导入sql脚本生成物理模型
- redis 资料
- <LeetCode OJ> (1 / 15 / 16 / 18) NSum问题集合
- Android列表滑动冲突原因和解决方法
- 【凯子哥带你夯实应用层】新手必备的常用代码片段整理(二)
- 关于python的编解码(decode, encode)