首页 / ANDROID / android 广播的使用
android 广播的使用
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了android 广播的使用,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2483字,纯文字阅读大概需要4分钟。
内容图文
在Activity中,注册广播的一个Demo。
总共分3步
第一步:定义一个BroadcastReceiver广播接收类:
[java] view plaincopy
- private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver(){
- @Override
- public void onReceive(Context context, Intent intent) {
- String action = intent.getAction();
- if(action.equals(ACTION_NAME)){
- Toast.makeText(Test.this, "处理action名字相对应的广播", 200);
- }
- }
- };
第二步:注册该广播:
[java] view plaincopy
- public void registerBoradcastReceiver(){
- IntentFilter myIntentFilter = new IntentFilter();
- myIntentFilter.addAction(ACTION_NAME);
- //注册广播
- registerReceiver(mBroadcastReceiver, myIntentFilter);
- }
第三步:触发响应
[java] view plaincopy
- mBtnMsgEvent = new Button( this );
- mBtnMsgEvent.setText("发送广播");
- mBtnMsgEvent.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent mIntent = new Intent(ACTION_NAME);
- mIntent.putExtra("yaner", "发送广播,相当于在这里传送数据");
- //发送广播
- sendBroadcast(mIntent);
- }
- });
-----最后附上完整代码:
[java] view plaincopy
- package my.yaner;
- import android.app.Activity;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.LinearLayout;
- import android.widget.Toast;
- public class Test extends Activity{
- private final String ACTION_NAME = "发送广播";
- private Button mBtnMsgEvent = null;
- protected void onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- //注册广播
- registerBoradcastReceiver();
- LinearLayout mLinearLayout = new LinearLayout(this);
- mBtnMsgEvent = new Button(this);
- mBtnMsgEvent.setText("发送广播");
- mLinearLayout.addView(mBtnMsgEvent);
- setContentView(mLinearLayout);
- mBtnMsgEvent.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent mIntent = new Intent(ACTION_NAME);
- mIntent.putExtra("yaner", "发送广播,相当于在这里传送数据");
- //发送广播
- sendBroadcast(mIntent);
- }
- });
- }
- private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver(){
- @Override
- public void onReceive(Context context, Intent intent) {
- String action = intent.getAction();
- if(action.equals(ACTION_NAME)){
- Toast.makeText(Test.this, "处理action名字相对应的广播", 200);
- }
- }
- };
- public void registerBoradcastReceiver(){
- IntentFilter myIntentFilter = new IntentFilter();
- myIntentFilter.addAction(ACTION_NAME);
- //注册广播
- registerReceiver(mBroadcastReceiver, myIntentFilter);
- }
- }
原文:http://blog.csdn.net/lsong89/article/details/39160427
内容总结
以上是互联网集市为您收集整理的android 广播的使用全部内容,希望文章能够帮你解决android 广播的使用所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。
来源:【匿名】