您的位置:首页 > 移动开发 > Android开发

Android 交互功能组件BroadcastReceiver 的简单使用

2014-04-24 09:20 387 查看
大多数简单的应用程序都是相互独立,相互隔离,而复杂的应用程序需要和硬件,原始组件产生交互。鉴于此,Android也提供了一些供应用程序交互的功能组件。

主要包括: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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐