下面几篇讲解广播知识点,本篇讲最简单的广播发送、收广播、静态注册广播、传数据(文件中的例子下载)
一、广播的机制讲参考其它资料,我这里不再讲解
二、发广播的过程
Intent intent = new Intent();//
intent.setAction("farsight.inf");//参数代表广播类型,是自定义的任意字符串
再调用ContextWrapper为中的sendBroadcast(Intent intent);//发广播
还可以装数据以后再发,如
Intent intent = new Intent();
intent.setAction("farsight.inf")
Bundle bundle = new Bundle();
bundle.putInt("age", 32);
bundle.putString("name", "小陶");
intent.putExtras(bundle);
sendBroadcast(Intent intent);
三、用广播监听器(BroadcastReceiver)收广播
当广播发出后,可以在发广播的同一台手机上的任何项目去监听该广播,当监听到即可收到广播
1.
写出广播监听器的类
public class MyBroadcastReceiver extends BroadcastReceiver {
/**当监听到广播以后,该函数被自动回调,当函数被调用了,也说明了监听到了广播*/
@Override
public void onReceive(Context context, Intent intent) {
//取出监听到的广播类型
String type = intent.getAction();//取出监听到的广播类型
if("farsight.inf".equals(type)) {
Toast.makeText(context, "监听到的广播类型是:"+type, Toast.LENGTH_LONG).show();
//取广播数据
Bundle bundle = intent.getExtras();
int age = bundle.getInt("age");
String name = bundle.getString("name");
Toast.makeText(context, "age:"+age+",name:"+name, Toast.LENGTH_LONG).show();
}
}
}
2.静态注册广播监听器类,即在AndroidManifest.xml文件的<aplication>标签中嵌套以下标签
<receiver android:name="监听器类的包名点类名">
<intent-filter>
<action android:name="该广播监听器监听什么样的广
播类型即intent.setAction("farsight.inf")中参数" />
</intent-filter>
</receiver>
3.完整示例:
(1)创建项目send
(2)send项目中main.xml文件内容
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/send"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="发广播"
/>
</LinearLayout>
(3)send项目中com.fs.activity.MainActivity类代码
package com.fs.activity;
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 implements OnClickListener {
private Button send;
private void init() {
this.send = (Button) this.findViewById(R.id.send);
this.send.setOnClickListener(this);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.init();
}
/**
* 发广播
*/
@Override
public void onClick(View v) {
String type = "farsight.inf";
Intent intent = new Intent();
intent.setAction(type);
Bundle bundle = new Bundle();
bundle.putInt("age", 32);
bundle.putString("name", "小陶");
intent.putExtras(bundle);
this.sendBroadcast(intent);//发广播
}
}
(4)创建项目receiver,在该项目中创建广播接收器监听并接收farsight.inf类型的广播,(该项目中我把
Activity类去掉了的)
在receiver项目中类com.fs.receiver.MyBroadcastReceiver的代码
public class MyBroadcastReceiver extends BroadcastReceiver {
/**当监听到广播以后,该函数被自动回调,当函数被调用了,也说明了监听到了广播*/
@Override
public void onReceive(Context context, Intent intent) {
//取出监听到的广播类型
String type = intent.getAction();//取出监听到的广播类型
if("farsight.inf".equals(type)) {
Toast.makeText(context, "监听到的广播类型是:"+type, Toast.LENGTH_LONG).show();
//取出广播数据
Bundle bundle = intent.getExtras();
int age = bundle.getInt("age");
String name = bundle.getString("name");
Toast.makeText(context, "age:"+age+",name:"+name, Toast.LENGTH_LONG).show();
}
}
}
(5)receiver项目中AndroidManifest.xml文件内容
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.fs.receiver.broadcast" android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<receiver android:name="com.fs.receiver.broadcast.broadcast.MyBroadcastReceiver">
<intent-filter>
<action android:name="farsight.inf" />
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="4" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
</manifest>
(6)测试,把两个项目安装在同一个手机中,然后打开send项目,点击其中的按钮,则接收器类MyBroadcastReceiver中的onReceive函数就会自动被调用
(7)完
Android广播基础_广播一
最新推荐文章于 2016-01-08 10:47:20 发布