思路是构造两个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>
广播入门了。
相关推荐
broadcast和service的demo
android 广播基础学习资源
广播和消息的混合使用,发送广播后,用notification提醒广播是否发送成功
Android BroadcastReceiver
Android Broadcast 静态注册Demo代码
广播演示 android服务演示
Android Broadcast 的简易使用例子
Android 广播接收 发送 Broadcast demo
在Android平台上实现UDP广播的发送和接收
完整DEMO,一个Activity,一个broadcast,一个service 程序最少在手机上运行一次,手机重启也可以启动服务
Android四大组件之一BroadcastReceiver实战demo,实现广播注册、发送、注销等功能,并给出实际开发过程中3种常见场景解决方案,1.如何监听网络变化?2.如何监听手机电量变化?3.如何监听app安装变化? 是学习...
一个关于Android四大组件Broadcast Receiver的小demo
Android利用广播BroadCast监听网络的变化。该Demo是网络资源整理,可以正常运行。你可以看到内部接口的使用,广播机制的使用,网络连接状态的检查等。
这是我在学习BroadcastReceiver时总结的一个Demo,现在分享出来方便有需要的人学习。
用户通过Broadcast(广播)进行与后台通信模块Service进行通信(控制Service或者接收反馈信息)
android BroadcastReceiver动态注册,实现接收发信息。
Android四大组件之一,BroadcastReceiver的实例Demo。
前几年的Android应用源码Demo,主要面向的是学生毕业设计学习。
详解三大组件的综合使用,广播的静态注册与动态注册,如何结合Service使用广播等