思路是构造两个activity一个activity设置为dialog主题,然后发送广播给另一个activity。
sendActivity:
public class sendBroadcast extends Activity { private Button sendButton = null; private EditText sendEditText = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_send); sendButton = (Button) findViewById(R.id.send_bt); sendEditText = (EditText) findViewById(R.id.send_et_content); sendButton.setOnClickListener(onClickListener); } android.view.View.OnClickListener onClickListener = new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.send_bt: sendBroadcast(sendEditText.getText().toString()); break; } } }; /** * 发送广播 * * @param string */ private void sendBroadcast(String string) { Intent intent = new Intent(); Bundle bundle = new Bundle(); bundle.putString("BroadcastContent", string); intent.putExtras(bundle); intent.setAction("Broadcast_Action");//给意图添加Action sendBroadcast.this.sendBroadcast(intent); } }
receiverActivity:
public class Receiver extends Activity {
private TextView showBroadcastContentTextView=null;
private Button showSendBroadcastActivityButton=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receiver);
showBroadcastContentTextView=(TextView)findViewById(R.id.receiver_tv_content);
showSendBroadcastActivityButton=(Button)findViewById(R.id.receiver_bt_showsendbroadcast);
showSendBroadcastActivityButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(Receiver.this,sendBroadcast.class);
Receiver.this.startActivity(intent);
}
});
}
//新建广播接收器,重写接受方法
private BroadcastReceiver broadcastReceiver=new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
//获取广播传递过来的数据,显示在textview中。
String contentString=intent.getStringExtra("BroadcastContent");
Toast.makeText(Receiver.this, contentString, Toast.LENGTH_LONG).show();
showBroadcastContentTextView.setText(contentString);
}
};
@Override
protected void onResume() {//生成activity时添加意图拦截器,拦截广播并注册广播接收器
// TODO Auto-generated method stub
IntentFilter intentFilter=new IntentFilter();
intentFilter.addAction("Broadcast_Action");
registerReceiver(broadcastReceiver, intentFilter);
super.onResume();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
if (broadcastReceiver!=null) {
unregisterReceiver(broadcastReceiver);//销毁时接触注册
}
super.onDestroy();
}
}
receiver_layout:
<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=".Receiver" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="BroadCastReceiver" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/receiver_tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/textView1" android:layout_below="@+id/textView1" android:layout_marginRight="20dp" android:layout_marginTop="24dp" /> <Button android:id="@+id/receiver_bt_showsendbroadcast" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginRight="30dp" android:text="ShowSendBroadcast" /> </RelativeLayout>
send_layout:
<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" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="19dp" android:layout_marginTop="23dp" android:text="broadCast使用" android:textAppearance="?android:attr/textAppearanceMedium" /> <EditText android:id="@+id/send_et_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_below="@+id/textView1" android:ems="10" > <requestFocus /> </EditText> <Button android:id="@+id/send_bt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/send_et_content" android:text="发送广播" /> </RelativeLayout>
广播入门了。