位置: 编程技术 - 正文

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)

  • 超标准能按小规模纳税人标准纳税吗?
  • 计算本月应交所得税
  • 进项税和销项税税率一样吗
  • 小微企业公司章程范本
  • 个人所得税租房专项扣除标准
  • 债务豁免需要缴纳什么税
  • 年终奖个人所得税计算器
  • 工会经费应税项怎么算
  • 员工交通费补贴标准
  • 财务报表中的负债是什么意思
  • 门面入股做生意
  • 汇算清缴期间费用社保填哪里
  • 收取质保金会计处理
  • 公司间分摊费用开票问题
  • 公司挂靠有资质的企业公司会计处理
  • 母子公司可以开具资金占用费
  • 加工开票税率是多少
  • 国税局预缴税款在哪里看
  • 销售已作进项税转出的固定资产怎样缴税?
  • 公司租赁办公场地用缴纳房产税吗
  • 付佣金代扣个人所得税税前列支
  • windows11怎么显示桌面图标
  • 在win7中如何找到WAN服务
  • 分享下会画画是怎样的体验
  • 仓储费计入存货成本吗
  • 土地使用税怎么交税
  • 有形动产增值税税率是多少
  • php assign
  • php验证码扭曲效果怎么做
  • 会计科目备抵科目都有哪些
  • laravel auth:api
  • 跨年度退货
  • 小规模纳税人出租不动产免征增值税
  • 其他应付款转入管理费用
  • py转换成exe后打开没用
  • js经典案例代码大全
  • 利润分配和所有权的关系
  • 企业所得税的计算公式三种
  • 盘亏的固定资产是资产吗
  • python字符串方法总结
  • vue–router
  • curl抓包
  • 科技型中小微企业贷款贴息贴保项目入库
  • sql server 2005 sp4
  • sql2008收缩日志文件
  • 出票后定期付款的汇票,其提示付款的期限为
  • 材料成本差异的超支与节约
  • 固定资产清理费用对应科目
  • 计提存货跌价准备计算公式
  • etc的充值发票可以报账吗
  • 将本月应交未交增值税转入未交增值税
  • 医院会计怎么做账
  • 未取得合法支付凭据和与本单位无关的收入
  • sql server的基本概念
  • linux统计重复次数
  • centos配置网络地址
  • windows查看电池信息
  • windowsxp忘了登录密码
  • 如何切换shell
  • win10触摸模式开启
  • win8 屏幕键盘
  • win7怎么设置u盘启动为第一启动项
  • win8如何输入命令
  • win8怎么切换界面
  • cocos2dx官方教程
  • windows安装node.js
  • css网站布局实录 pdf
  • css vh兼容性
  • 深入浅出Struts
  • nodejs基础知识
  • python做应用软件界面
  • JavaScipt中Function()函数的使用教程
  • jQuery progressbar通过Ajax请求实现后台进度实时功能
  • node.js express koa
  • python的爬虫技术
  • 黑马程序员培训怎样
  • 滴滴排队机制怎么设置
  • 第三方审计报告需要多久
  • 智行火车票电子报销凭证
  • 重庆税务信息采集如何操作
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设