android基础:广播BroadCast简单案例
2015-07-19 22:55
691 查看
运行效果
![](http://images0.cnblogs.com/blog2015/787177/201507/192249581101774.png)
string.xml
activity_main.xml
MainActivity
创建广播接听对象
AndroidMainfest.xml
案例实现小结:
1、定义ACTION,并建立发送Intent,调用sendBroadcast方法发送广播;
2、创建广播接收类。类要继承BroadcastReceiver,并重写onReceive方法;
3、在AndroidManifest.xml中注册广播接收对象,在intent-filter中添加ACTION。
![](http://images0.cnblogs.com/blog2015/787177/201507/192249581101774.png)
string.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">TestBroadcast</string> <string name="param_input">请输入发送参数</string> <string name="send_button">发送广播</string> <string name="action_settings">Settings</string> </resources>
activity_main.xml
<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="com.mytest.testbroadcast.MainActivity" > <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="@string/param_input" android:id="@+id/param_input"/> <Button android:id="@+id/send_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/param_input" android:text="@string/send_button" android:onClick="SendBroadCast" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/param_input" android:text="@null" android:id="@+id/view_result" /> </RelativeLayout>
MainActivity
package com.mytest.testbroadcast; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private EditText paramText; private TextView resultView; private final String ACTION_INTENT_TEST="com.mytest.testbroad.intent"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); paramText = (EditText) this.findViewById(R.id.param_input); resultView = (TextView) this.findViewById(R.id.view_result); } /** * 发送广播按钮onclick事件 * @param view */ public void SendBroadCast(View view){ String param = paramText.getText().toString(); //创建发送intent Intent intent = new Intent(this.ACTION_INTENT_TEST); //绑定参数 intent.putExtra("user_input", param); //发送广播 this.sendBroadcast(intent); resultView.setText("发送广播成功,参数值为:"+param); } }
创建广播接听对象
package com.mytest.testbroadcast; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class MyBroadCastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub String user_input = intent.getStringExtra("user_input"); Toast.makeText(context,"接收到广播,得到参数值为"+user_input,Toast.LENGTH_SHORT).show(); } }
AndroidMainfest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.mytest.testbroadcast" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="21" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".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> <!-- 注册广播接收类 --> <receiver android:name=".MyBroadCastReceiver"> <intent-filter> <action android:name="com.mytest.testbroad.intent"/> </intent-filter> </receiver> </application> </manifest>
案例实现小结:
1、定义ACTION,并建立发送Intent,调用sendBroadcast方法发送广播;
2、创建广播接收类。类要继承BroadcastReceiver,并重写onReceive方法;
3、在AndroidManifest.xml中注册广播接收对象,在intent-filter中添加ACTION。
相关文章推荐
- Android 屏幕适配
- 仪表图的实现--Android
- Android Studio学习笔记——构建系统
- Android屏幕适配全攻略
- android按键精灵学习_框架搭建
- android5.0使用svg
- 我的Android进阶之旅------>Android嵌入图像InsetDrawable的用法
- Android布局控件LinearLayout
- Android测试游戏帧数的方法
- Android NDK环境搭建(Android Studio)
- 我的第一个Android JNI程序
- android基础篇:提示框AlertDialog实例
- android基础:handler与messag案例(计时器)
- 迅雷下载Android SDK
- Android应用的耗电量统计
- Android JSON解析库的使用
- Android Common 整理
- Android用户登录机制安全性的一些思考
- Android Graphic 翻译系列
- Android屏幕适配