Android学习之BroadcastReceiver
2015-12-15 20:43
435 查看
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)
2. InValidateReceiver.java ( src/com.example.broadcast.InValidateReceiver.java)
3. ValidateReceiver.java ( src/com.example.broadcast.ValidateReceiver.java)
4. activity_main.xml -->MainActivity的布局文件
5. AndroidManifest.xml
从结构上可知接收器只接收与自己有相同的action的广播,所以这里ValidateReceiver接收广播信息,而不是InValidateReceive。
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。
相关文章推荐
- Android添加快捷方式(CutShort)到手机桌面
- Android中onTouch方法的执行过程以及和onClick执行发生冲突的解决办法
- Android之如果解决Android studio项目里面的类名不能重命名
- js调用android原生方法,简单明了。
- android 环境配置
- ExpandableListView
- 一些Android框架
- "Check Android SDK" has encountered a problem
- Android 性能测试实践(一)
- Android加载asset的db
- Android界面组件基本用法
- 图片_ _Android有效解决加载大图片时内存溢出的问题
- TextureView与Camera
- 从android studio(for mac)中导入APIdemo
- Android Studio错误信息记录[记录]
- Android屏幕适配全攻略(最权威的官方适配指导)
- android 颜色对照
- Android 屏幕适配方案
- 在android中调用jni,出现ReferenceTable overflow (max=1024)
- Android 实现ListView的A-Z字母排序和过滤搜索功能,实现汉字转成拼音