位置: 编程技术 - 正文

Android学习之BroadcastReceiver总结(android break)

编辑:rootadmin

推荐整理分享Android学习之BroadcastReceiver总结(android break),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:android中broadcastreceiver,androidobb,android中broadcastreceiver,android bsd,android中broadcastreceiver,android.bp详解,android.bp详解,android ble,内容如对您有帮助,希望把文章链接给更多的朋友!

Android学习之BroadcastReceiver总结

代码下载: 系统广播事件,比如:ACTION_BOOT_COMPLETED(系统启动完成后触发),ACTION_TIME_CHANGED(系统时间改变时触发),ACTION_BATTERY_LOW(电量低时触发)等等。

② 用户自定义的广播事件。

BroadcastReceiver事件的编程流程

① 注册广播事件:注册方式有两种,一种是静态注册,就是在 AndroidManifest.xml文件中定义,注册的广播接收器必须要继承BroadcastReceiver类;另一种是动态注册,是在程序中使用Context.registerReceiver注册,注册的广播接收器相当于一个匿名类。两种方式都需要IntentFIlter。

② 发送广播事件:通过Context.sendBroadcast来发送,由Intent来传递注册时用到的Action。

③ 接收广播事件:当发送的广播被接收器监听到后,会调用它的onReceive()方法,并将包含消息的Intent对象传给它。onReceive中代码的执行时间不要超过5s,否则Android会弹出超时dialog。

BroadcastReceiver事件的编程举例

说明:该项目举例说明了4种情况的广播事件,静态注册的系统广播事件、静态注册的用户自定义广播事件、动态注册的系统广播事件和动态注册的用户自定义广播事件。

1. MainActivity.java文件内容

package com.byread;

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.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.Toast;

publicclass MainActivityextends Activity {

private ButtonsendStaticBtn;

private ButtonsendDynamicBtn;

private ButtonsendSystemBtn;

privatestaticfinal StringSTATICACTION ="com.byread.static";

privatestaticfinal StringDYNAMICACTION ="com.byread.dynamic";

//系统Action:

privatestaticfinal StringSYSTEMACTION = Intent.ACTION_BATTERY_CHANGED;

//重写Activity的onCreate方法

@Override

publicvoid onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

sendStaticBtn = (Button)findViewById(R.id.send_static);

sendDynamicBtn = (Button)findViewById(R.id.send_dynamic);

sendSystemBtn = (Button)findViewById(R.id.send_system);

sendStaticBtn.setOnClickListener(newMyOnClickListener());

sendDynamicBtn.setOnClickListener(newMyOnClickListener());

sendSystemBtn.setOnClickListener(newMyOnClickListener());

}

//内部类,用于监听按钮消息

class MyOnClickListenerimplements OnClickListener{

@Override

publicvoid onClick(View v) {

//发送自定义静态注册广播消息

if(v.getId() == R.id.send_static){

Log.e("MainActivity","发送自定义静态注册广播消息");

Intentintent =newIntent();

intent.setAction(STATICACTION);

intent.putExtra("msg","接收静态注册广播成功!");

sendBroadcast(intent);

}

//发送自定义动态注册广播消息

elseif(v.getId() == R.id.send_dynamic){

Log.e("MainActivity","发送自定义动态注册广播消息");

Intentintent =newIntent();

intent.setAction(DYNAMICACTION);

intent.putExtra("msg","接收动态注册广播成功!");

sendBroadcast(intent);

}

// 发送系统动态注册广播消息。当手机连接充电设备时会由系统自己发送广播消息。

elseif(v.getId() == R.id.send_system){

Log.e("MainActivity","发送系统动态注册广播消息");

Intentintent =newIntent();

intent.setAction(SYSTEMACTION);

intent.putExtra("msg","正在充电。。。。");

}

}

}

//重写MainActivity的onStart()函数

@Override

protectedvoid onStart() {

super.onStart();

Log.e("MainActivity","注册广播事件");

// 1注册自定义动态广播消息

IntentFilterfilter_dynamic =new IntentFilter();

filter_dynamic.addAction(DYNAMICACTION);

registerReceiver(dynamicReceiver, filter_dynamic);

// 1注册系统动态广播消息

IntentFilterfilter_system =new IntentFilter();

filter_system.addAction(SYSTEMACTION);

registerReceiver(systemReceiver, filter_system);

}

// 2自定义动态广播接收器,内部类

private BroadcastReceiverdynamicReceiver =new BroadcastReceiver() {

@Override

publicvoid onReceive(Context context, Intent intent) {

Log.e("MainActivity","接收自定义动态注册广播消息");

if(intent.getAction().equals(DYNAMICACTION)){

String msg = intent.getStringExtra("msg");

Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();

}

}

};

// 2 系统动态广播接收器,内部类

private BroadcastReceiversystemReceiver =new BroadcastReceiver() {

@Override

Android学习之BroadcastReceiver总结(android break)

publicvoid onReceive(Context context, Intent intent) {

Log.e("MainActivity","接收系统动态注册广播消息");

if(intent.getAction().equals(SYSTEMACTION)){

String msg = intent.getStringExtra("msg");

Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();

}

}

};

}

2. 自定义静态注册广播消息接收器

package com.byread;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.widget.Toast;

/**

*自定义静态注册广播消息接收器

* @authorzuolongsnail

*

*/

publicclass StaticReceiverextends BroadcastReceiver {

@Override

publicvoid onReceive(Context context, Intent intent) {

String msg= intent.getStringExtra("msg");

Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();

}

}

3. 系统静态注册广播消息接收器

package com.byread;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.util.Log;

import android.widget.Toast;

/**

*系统静态注册广播消息接收器

*

* @authorzuolongsnail

*

*/

publicclass SystemReceiverextends BroadcastReceiver {

@Override

publicvoid onReceive(Context context, Intent intent) {

if(intent.getAction().equals(Intent.ACTION_BATTERY_LOW)) {

Log.e("SystemReceiver","电量低提示");

Toast.makeText(context,"您的手机电量偏低,请及时充电", Toast.LENGTH_SHORT).show();

}

}

}

4. main.xml布局文件

<?xmlversion="1.0"encoding="utf-8"?>

<LinearLayoutxmlns:android=" android:orientation="vertical"android:layout_width="fill_parent"

android:layout_height="fill_parent">

<TextViewandroid:layout_width="fill_parent"

android:layout_height="wrap_content"android:text="@string/hello"/>

<Buttonandroid:id="@+id/send_static"android:layout_width="wrap_content"

android:layout_height="wrap_content"android:text="发送自定义静态注册广播"/>

<Buttonandroid:id="@+id/send_dynamic"android:layout_width="wrap_content"

android:layout_height="wrap_content"android:text="发送自定义动态注册广播"/>

<Buttonandroid:id="@+id/send_system"android:layout_width="wrap_content"

android:layout_height="wrap_content"android:text="发送系统动态注册广播"/>

</LinearLayout>

5. AndroidManifest.xml文件

<?xmlversion="1.0"encoding="utf-8"?>

<manifestxmlns:android=" package="com.byread"android:versionCode="1"android:versionName="1.0">

<applicationandroid:icon="@drawable/icon"android:label="@string/app_name">

<activityandroid:name=".MainActivity"android:label="@string/app_name">

<intent-filter>

<actionandroid:name="android.intent.action.MAIN"/>

<categoryandroid:name="android.intent.category.LAUNCHER"/>

</intent-filter>

</activity>

<!--注册自定义静态广播接收器-->

<receiverandroid:name=".StaticReceiver">

<intent-filter>

<actionandroid:name="com.byread.static"/>

</intent-filter>

</receiver>

<!--注册系统静态广播接收器-->

<receiverandroid:name=".SystemReceiver">

<intent-filter>

<actionandroid:name="android.intent.action.BATTERY_LOW"/>

</intent-filter>

</receiver>

</application>

</manifest>

模块二:LocalBroadcastManager

优点:

1.应用内的广播,比Broadcast更安全

2.不牵涉到进程间通信,更有效

3.更安全

使用方式:

1.注册:

2.写一个广播接收器:

3.发送广播

4.注销广播:

android学习之选择媒体库中的图片 从媒体库中选择图片主要是启动系统相关ActivityIntenti=newIntent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;startActivityForResult(i,RESULT_LOAD_IM

Android开发之文件浏览器 源码下载地址:

android学习之ListView总结 ListView总结在原来的基础上,增加了点击后的消息响应函数。源代码下载:

标签: android break

本文链接地址:https://www.jiuchutong.com/biancheng/368655.html 转载请保留说明!

上一篇:学习android之Service(android secure)

下一篇:android学习之选择媒体库中的图片(android xui)

  • 怎样理解一般纳税人
  • 报税怎么操作流程图
  • 有金额的框架协议要交印花税吗
  • 办公室租金发票可以扺军品企业增值税吗
  • 其他综合收益为什么是所有者权益
  • 一般纳税人普票怎么做分录
  • 出纳单据交接表怎么填写
  • 转让不动产取得的发票
  • 债务重组损失计算公式
  • 设备安装行业前景
  • 清算资金往来借贷方什么意思
  • 进口货物的价格国内销售需要考虑的税
  • 预交了一个季度的社保
  • 水电费大于发票怎么处理?
  • 工资未发放是否可以仲裁
  • 可以给行政事业单位开增值税专用发票吗
  • 附加税是当月计提当月缴纳吗
  • 发票作废情况说明书
  • 公司的差旅费
  • 快递费可以抵扣进项税额吗
  • 交强险 收费
  • 房地产中介公司排名
  • 个人收回转让的股权个税应如何处理?
  • 调研费用表格
  • 金蝶核算项目明细表
  • php get post
  • Fort Rock Valley Historical Homestead博物馆,俄勒冈 (© Prisma by Dukas Presseagentur GmbH/Alamy)
  • 应缴纳的房产税计入什么科目
  • 无限极分类mysql
  • 新手会计怎么做帐
  • phpjson
  • 增值税的纳税时间是多久
  • vue.js in action
  • 国际货运代理公司排名
  • 小规模纳税人定义
  • 为员工购买意外险会计处理
  • 进项税额转出忘记申报咋办
  • 应付账款主要账户有哪些
  • 房开企业预缴增值税附加税费
  • 房地产行业概况
  • 什么情况下一般疑问句用does
  • 普通发票怎么冲红字
  • 固定资产出售的收入属于收入吗
  • 备用金怎么计入明细账
  • 直接人工成本包括管理人员开的工资吗
  • 销售货物的流程
  • 民办非企业单位什么意思
  • 总包分包怎么区分
  • 购销业务活动包括哪些
  • 资产状况信息公示还是不公示
  • mysql类型varchar长度
  • mysql锁表的sql
  • mysql数据监控
  • mysqld占用cpu高
  • win10预览版21390
  • 使用win10
  • smsvchost.exe是什么文件 电脑中Svchost.exe病毒解决办法
  • 电脑出现黑屏英文要怎么处理
  • win7电脑怎么设置
  • 如何进入win10安装界面
  • mac视频预览图不显示
  • mediaexplorer是什么意思
  • win10原装版
  • win8系统如何恢复出厂设置
  • win7开机自动弹出注册表编辑器怎么办
  • opengl示例
  • JavaScript中的数据类型分为两大类
  • JavaScript_ECMA5数组新特性详解
  • Android游戏开发读后感
  • cocos2dx 2.2.2
  • unity游戏官网
  • android新手入门
  • javascriptcsdn
  • java颜色代码对照表图片
  • android自定义组件开发详解
  • shell脚本编程实例
  • 长春市国资局
  • 缴款单号怎么交学费
  • 信用社股金转让 办理要多少时间
  • 小企业会计准则会计科目表
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设