Android学习之本地广播使用方法详解
时间:2022-07-28
本文章向大家介绍Android学习之本地广播使用方法详解,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
本地广播信息只能在应用程序内部传递,同时广播接收器也只能接收应用程序内部的广播消息。
MainActivity代码
package com.example.luobo.mybroadcastreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button button;
private IntentFilter intentFilter;
private LocalBroadcastManager localBroadcastManager ;
private LocalReceiver localReciiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.send_button);
button.setOnClickListener(this);
localBroadcastManager = LocalBroadcastManager.getInstance(this);//使用
intentFilter = new IntentFilter();
intentFilter.addAction("com.example.luobo.mybroadcastreceiver.LOCAL_BROADCAST");
localReciiver = new LocalReceiver();
localBroadcastManager.registerReceiver(localReciiver,intentFilter);
}
@Override
protected void onDestroy() {
super.onDestroy();
localBroadcastManager.unregisterReceiver(localReciiver);
}
@Override
public void onClick(View view) {
Intent intent = new Intent("com.example.luobo.mybroadcastreceiver.LOCAL_BROADCAST");
localBroadcastManager.sendBroadcast(intent);
}
class LocalReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"received local broadcast",Toast.LENGTH_SHORT).show();
}
}
}
首先通过LocalBroadcastManager(本地广播管理类)的getInstance(this)方法获取实例,注册广播消息时是调用localBroadcastManager实例的registerReceiver(参数1,参数2)方法注册(参数1是本地广播接受者,参数2是过滤器只选择接收特定的广播消息),调用localBroadcastManager实例的sendBroadcast(Initent initent)方法发送广播消息。
MyRecevity
package com.example.luobo.mybroadcastreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"Received in MyBroadCastReceiver",Toast.LENGTH_SHORT).show();
abortBroadcast();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.luobo.mybroadcastreceiver.MainActivity"
<Button
android:id="@+id/send_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送广播"/
</android.support.constraint.ConstraintLayout
AndroidMainfest.aml
<?xml version="1.0" encoding="utf-8"?
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.luobo.mybroadcastreceiver"
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
<activity android:name=".MainActivity"
<intent-filter
<action android:name="android.intent.action.MAIN" /
<category android:name="android.intent.category.LAUNCHER" /
</intent-filter
</activity
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true"
<intent-filter
android:priority="100"
<action android:name="com.example.luobo.mybroadcastreceiver.LOCAL_BROADCAST"/
</intent-filter
</receiver
</application
</manifest
以上就是本文的全部内容,希望对大家的学习有所帮助。
- Android网络编程(十一)源码解析Retrofit
- android Material Design详解
- android EventBus详解(三)
- Android绘制优化(一)绘制性能分析
- android EventBus详解(二)
- [置顶] android EventBus详解(一)
- 开源组件photoView学习
- ViewPagerIndicator+viewpager指示器详解
- 实现去哪儿来回机票选择的view
- 解决水平ListView在ScrollView中出现的滑动冲突
- Android逆向分析(2) APK的打包与安装背后的故事
- Activity之间传递大数据问题
- React Native入门(二)Atom+Nuclide安装、配置与调试
- React Native入门(一)环境搭建与Hello World
- java教程
- Java快速入门
- Java 开发环境配置
- Java基本语法
- Java 对象和类
- Java 基本数据类型
- Java 变量类型
- Java 修饰符
- Java 运算符
- Java 循环结构
- Java 分支结构
- Java Number类
- Java Character类
- Java String类
- Java StringBuffer和StringBuilder类
- Java 数组
- Java 日期时间
- Java 正则表达式
- Java 方法
- Java 流(Stream)、文件(File)和IO
- Java 异常处理
- Java 继承
- Java 重写(Override)与重载(Overload)
- Java 多态
- Java 抽象类
- Java 封装
- Java 接口
- Java 包(package)
- Java 数据结构
- Java 集合框架
- Java 泛型
- Java 序列化
- Java 网络编程
- Java 发送邮件
- Java 多线程编程
- Java Applet基础
- Java 文档注释
- (Demo分享)利用JavaScript(JS)实现一个九宫格拖拽功能
- 第19天:NLP实战(三)——用CNN实现微博谣言检测
- (Demo分享)利用原生JavaScript-ScrollLeft-实现做轮播广告通知
- Flutter问题:Column里面嵌套两个SingleChildScrollView无法滚动
- 搞不懂JS中赋值·浅拷贝·深拷贝的请看这里
- 第20天:NLP实战(四)——用GRU模型实现电影评论情感分析
- Flutter SingleChildScrollView 滚动控件
- Flutter Stack、Positioned 层叠布局
- Flutter Row、Column 线性布局
- 第21天:NLP实战(五)——词向量Skip-gram实践
- ES10(2019)有哪些更新和新特性?
- Flutter Icon IconFont(图标控件)
- Flutter TextField(输入控件)
- 常见的面试问题总结
- Flutter Button(按钮控件)