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

【Android】Broadcast Receiver的基本使用,推一条广播给多个Receiver

2015-09-30 11:26 453 查看
Android所谓的四大组件:Activity,这个大家只要会写Android程序都知道是一个界面的意思,Service,这个专门用来用来后台推送,就是一条没有界面的线程,具体在《【Android】揭秘如何利用Service与线程制造安卓后台通知栏推送 》(点击打开链接)已经介绍过了,Content Provider这个也很常用,可以快速地直接拿内存卡的数据、Sqlite数据库中的数据,具体在《【Android】利用安卓的数据接口、多媒体处理编写内存卡Mp3播放器app》(点击打开链接)与《【Android】Sqlite数据库增删改查》(点击打开链接)中都已经讲过了。唯独只有广播器Broadcast
Receiver少用,数安卓四大组件的时候经常忘记,其实这东西,说白的,就是专门在一堆Java文件中传递数据的。

如下图的一个安卓工程,如果你需要在MainActivity中发送一条信息,同时给Receiver1、2、3三个Java文件收到,然后这3个Java文件同时收到广播之后该干嘛干嘛,那么Broadcast Receiver就派上用场了。其中Receiver1、2、3三个Java文件没有界面,类似于Service,专门用来处理一些后台数据。



比如如下的一个例子。在MainActivity.java点击“发送广播”的按钮,Receiver1、2、3得到广播信息之后,则会Toast信息,基本上,Receiver1、2、3是同时Toast信息的。



制作过程如下,首先是一些比较杂七杂八的部分,在res\values\strings.xml中设置按钮的字体:

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">BroadcastReceiverTest</string>
<string name="action_settings">Settings</string>
<string name="button1">发送广播</string>

</resources>


在res\layout\activity_main.xml中给MainActivity.java布置一个带ID按钮:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
android:id="@+id/Button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/button1"
android:textSize="24sp" />

</LinearLayout>

MainActivity.java获取这个按钮之后,设置其点击事件是发送广播,广播的标识是android.intent.action.MY_BROADCAST,内容是“呵呵”,相当于现实世界的频段。不是这个频段的Java文件中Broadcast Receiver,是无法接受到“呵呵”信息的。如同《【Android】多个Activity之间利用bundle传递数值》(点击打开链接)一样,这里的信息载体还是intent。sendBroadcast是发送广播,安卓中特定的方法可以直接使用,将这个intent发送给标识是android.intent.action.MY_BROADCAST的Receiver。
package com.broadcastreceivertest;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;
import android.content.Intent;

public class MainActivity extends Activity {
private Button button1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.Button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("android.intent.action.MY_BROADCAST");
intent.putExtra("msg", "呵呵!");
sendBroadcast(intent);// 直接就是一个特定方法可以直接使用~
}
});
}
}


接下来,在AndroidManifest.xml注册三个标识是android.intent.action.MY_BROADCAST的Receiver,指明其接受的Java文件,也就是所谓的类,还有广播标识。如同注册Activity一样。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.broadcastreceivertest"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.broadcastreceivertest.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>

<!-- 注册3个广播接收器 -->
<!-- 接收器1 -->
<receiver android:name=".Receiver1" > <!-- 接收的Java文件 -->
<intent-filter>
<action android:name="android.intent.action.MY_BROADCAST" /> <!-- 广播器的标识(频段) -->

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<!-- 接收器2 -->
<receiver android:name=".Receiver2" >
<intent-filter>
<action android:name="android.intent.action.MY_BROADCAST" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<!-- 接收器3 -->
<receiver android:name=".Receiver3" >
<intent-filter>
<action android:name="android.intent.action.MY_BROADCAST" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>

</manifest>

之后,在主包com.broadcastreceivertest如下图新建3个Java文件Receiver1、2、3,皆继承android.content.BroadcastReceiver



其代码如下:

Receiver1.java

package com.broadcastreceivertest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class Receiver1 extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
String msg = intent.getStringExtra("msg");
Toast.makeText(context, "Receiver1得到信息:"+msg, Toast.LENGTH_SHORT).show();
}

}


Receiver2.java
package com.broadcastreceivertest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class Receiver2 extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
String msg = intent.getStringExtra("msg");
Toast.makeText(context, "Receiver2得到信息:"+msg, Toast.LENGTH_SHORT).show();
}

}


Receiver3.java
package com.broadcastreceivertest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class Receiver3 extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
String msg = intent.getStringExtra("msg");
Toast.makeText(context, "Receiver3得到信息:"+msg, Toast.LENGTH_SHORT).show();
}

}


可以看到这3个Java文件的结构都是类似的,重写onReceive方法中的context参数就是该Java文件在安卓系统的上下文,Toast等需求上下文来实现的东东,就用直接用此上下文即可,之后的intent,就是广播信息传递过来的intent,用intent.getStringExtra就能解释其中的key-value对。
在结果可以看出以上利用了Broadcast Receiver完成MainActivity.java对Receiver1、2、3.java的操控。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: