<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<EditText
android:id="@+id/Eview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/broadcast"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_below="@id/Eview"
android:layout_alignLeft="@id/Eview"
android:onClick="clickbroadcast"
android:text="发送"
/>
</RelativeLayout>
?package com.example.broadcast;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
/**
*
* @author Administrator
*广播的发送
*/
public class MainActivity extends Activity {
private EditText eView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//获得布局
setContentView(R.layout.activity_main);
//获取布局中的组件的id
eView=(EditText)this.findViewById(R.id.Eview);
}
//按钮的监听器
//发送广播,使用Intent意图进行发送
public void clickbroadcast(View v){
//获取输入框的值
String str=eView.getText().toString().trim();
//创建意图对象,发送数据
Intent intent = new Intent();
//设置广播的频率
intent.setAction("com.cn");
System.out.println("+++++");
//intent传递数据
intent.putExtra("msg", str);
//发送普通广播
//this.sendBroadcast(intent);
//发送有序广播
this.sendOrderedBroadcast(intent, null);
}
}
?package com.example.broadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
/**
*
* @author Administrator
*1,使用广播接收数据 继承BroadcastReceiver
*2,重写BroadcastReceiver类中的onReceive方法
*3,注册广播
* a,在清单文件中注册(本程序使用)
* b,在代码中动态注册
*/
public class MyBroadService extends BroadcastReceiver {
//广播的程序是自己执行的,不需要我们手动操作,只需要订阅号相关的配置就可以了(使用意图过滤器配置一个Action);
@Override
public void onReceive(Context context, Intent intent) {
//接收intent传送的参数
String str=intent.getStringExtra("msg");
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
}
?<receiver
android:name=".MyBroadService">
<intent-filter android:priority="100">
<action android:name="com.cn"
/>
</intent-filter>
</receiver>
?<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/Eview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
?package com.example.receiver;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.widget.EditText;
/*
* 数据的显示处理类
*/
public class MainActivity extends Activity {
private EditText eView;
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
eView = (EditText) this.findViewById(R.id.Eview);
// 使用handler将广播接收到的数据显示出来
handler = new Handler() {
//重写handler的方法
@Override
public void handleMessage(Message msg) {
//判断what的值
switch (msg.what) {
case 100:
String msgs = (String) msg.obj;
eView.setText(msgs);
break;
}
}
};
// 动态创建意图过滤器
IntentFilter filter = new IntentFilter();
// 设置发送方的广播频率
filter.addAction("com.cn");
//设置有序广播的优先级别
filter.setPriority(1000);
// 创建 广播类
MyBroadCastRecever receiver = new MyBroadCastRecever(handler);
// 注册广播
this.registerReceiver(receiver, filter);
}
}
?package com.example.receiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
//广播的动态定义
public class MyBroadCastRecever extends BroadcastReceiver{
private Handler handler;
public MyBroadCastRecever(Handler handler){
this.handler=handler;
}
@Override
public void onReceive(Context context, Intent intent) {
String str =intent.getStringExtra("msg");
//创建消息对象保存数据 Message对象需要一个what 表示
Message msg =handler.obtainMessage();
msg.what=100;
msg.obj=str;
//使用handler发送数据,需要一个Message对象,创建一个Message对象
handler.sendMessage(msg);
}
}
? <receiver android:name=".MyBroadCastRecever">
</receiver>
?原文:http://baihe747.iteye.com/blog/2188531