Android 交互功能组件BroadcastReceiver 的简单使用
2014-04-24 09:20
387 查看
大多数简单的应用程序都是相互独立,相互隔离,而复杂的应用程序需要和硬件,原始组件产生交互。鉴于此,Android也提供了一些供应用程序交互的功能组件。
主要包括:BroadcastReceiver , Intent, Adapters,ContentProviders。
本篇简单讲述BroadcastReceiver的使用,记录自己的学习经历。
BroadcastReceiver使用主要包含几个部分,一是继承BroadcastReceiver类,实现onReceive方法;二是广播消息发送的源头(sendBroadcast);三是数据信息的传递
首先,实现一个IntentTestActivity继承Activity,添加一个按钮,单击后发送一个广播消息
说明:发送广播消息以Intent为桥梁,将此次Intent意图对应的action行为发送给广播接收者,对于数据的传递putExtra( )可以传递多种类型的数据以及bundle
接着,实现广播接收器MyReceiver,继承BroadcastReceiver,实现onReceive接口,来完成广播的接收,并作相应的操作
说明:完成广播接收主要是onReceive的实现,由于是无向的,接收到的Intent可以是来自己任意一个发送者传递过来的,此处只需要关注对应action的Intent做出反应。
针对发送过来的数据,通过getExtra() 可以获取返回一个Bundle对象,然后通过Bundle的方法来获取最终的值,通过getAction返回收到的当前Intent行为是什么。
最后,还要在应用程序当中注册接收器能监听的action,此时需要在Androidmainifest.xml中设置receiver
主要包括:BroadcastReceiver , Intent, Adapters,ContentProviders。
本篇简单讲述BroadcastReceiver的使用,记录自己的学习经历。
BroadcastReceiver使用主要包含几个部分,一是继承BroadcastReceiver类,实现onReceive方法;二是广播消息发送的源头(sendBroadcast);三是数据信息的传递
首先,实现一个IntentTestActivity继承Activity,添加一个按钮,单击后发送一个广播消息
package com.android.test; import android.app.Activity; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class IntentTestActivity extends Activity { /** Called when the activity is first created. */ private static Button button = null; private static int nCount = 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); button = (Button)findViewById(R.id.send_button); button.setBackgroundColor(Color.GRAY); button.setHighlightColor(Color.RED); button.setOnClickListener(new OnClickListener() { // 添加按键监听器 public void onClick(View v) { // TODO Auto-generated method stub Intent itent = new Intent(); itent.setAction("com.android.IntetTest"); // 指定Intent的action 行为 itent.putExtra("data", nCount++);// 此处为了便于测试发送一个整数 IntentTestActivity.this.sendBroadcast(itent); // 通过Intent来启动广播并发送广播消息 Toast.makeText(IntentTestActivity.this,"发送广播消息"+"["+nCount+"]", Toast.LENGTH_SHORT).show(); } }); setContentView(R.layout.main); } }
说明:发送广播消息以Intent为桥梁,将此次Intent意图对应的action行为发送给广播接收者,对于数据的传递putExtra( )可以传递多种类型的数据以及bundle
接着,实现广播接收器MyReceiver,继承BroadcastReceiver,实现onReceive接口,来完成广播的接收,并作相应的操作
package com.android.test; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub if(intent.getAction().toString().equals("com.android.IntetTest")){ // 注意此处的Intent的action要对应广播发送者发送时的action int value = intent.getExtras().getInt("data"); // 接收广播中传送的数据 // 收到信息之后显示一下,可做其他对应操作 Toast.makeText(context, "收到广播消息"+"["+value+"]", Toast.LENGTH_SHORT).show(); } } }
说明:完成广播接收主要是onReceive的实现,由于是无向的,接收到的Intent可以是来自己任意一个发送者传递过来的,此处只需要关注对应action的Intent做出反应。
针对发送过来的数据,通过getExtra() 可以获取返回一个Bundle对象,然后通过Bundle的方法来获取最终的值,通过getAction返回收到的当前Intent行为是什么。
最后,还要在应用程序当中注册接收器能监听的action,此时需要在Androidmainifest.xml中设置receiver
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.test" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".IntentTestActivity" 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=".MyReceive"> <intent-filter> <!-- 此处监听的action也就是onReceiver中能收到的广播消息,当然在intent-filter中可以添加多个action --> <action android:name="om.android.IntetTest" /> </intent-filter> </receiver> </application> </manifest>
相关文章推荐
- Android网络应用之使用Scoket的Android客户端与TCP服务器的简单交互
- 使用websocket的组件socket.io实现最简单的聊天室功能。
- android使用ComponentName组件简单示例
- Android开发之CheckBox的简单使用与监听功能示例
- Android新组件RecyclerView的简单使用
- android四大组件之Service 模仿支付功能(AIDL使用)
- android-百度地图定位功能简单使用(一)
- android使用ComponentName组件简单示例
- Android recording 录音功能 简单使用小实例
- Android Design Support Library 中控件的使用简单介绍(一)介绍 在这个 Lib 中主要包含了 8 个新的 material design 组件!最低支持 Android
- Android中使用GridView和ImageViewSwitcher实现电子相册简单功能实例
- Android的计时组件Chronometer类简单使用
- 如何使用友盟 Cocos2d-x 分享组件实现 Android/iOS 分享功能
- 在Eclipse或者ADT中使用ButterKnifeZelezny,Android组件初始化从此变得简单易懂!!!!
- Android ExpandableListView简单使用(一)之仿QQ添加分组功能
- Android recording 录音功能 简单使用小实例
- Android客户端使用HttpURLConnection doGet与服务器数据交互简单示例
- Vue的路由功能(1)简单路由与vue-router的基础使用(同步/异步组件)
- 如何使用友盟 Cocos2d-x 分享组件实现 Android/iOS 分享功能
- Android开发之--(WIFI,蓝牙,电池,背光,SD卡,摄像头,按键,MIC,重力感应等)功能的简单使用