欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新
页面位置 : > > > 内容正文

Android四大组件之广播BroadcastReceiver详解,

来源: 开发者 投稿于  被查看 13978 次 评论:142

Android四大组件之广播BroadcastReceiver详解,


定义

BroadcastReceiver,“广播接收者”的意思,顾名思义,它就是用来接收来自系统和应用中的广播。在Android系统中,广播体现在方方面面,例如当开机完成后系统会产生一条广播,接收到这条广播就能实现开机启动服务的功能;当网络状态改变时系统会产生一条广播,接收到这条广播就能及时地做出提示和保存数据等操作;当电池电量改变时,系统会产生一条广播,接收到这条广播就能在电量低时告知用户及时保存进度等等。Android中的广播机制设计的非常出色,很多事情原本需要开发者亲自操作的,现在只需等待广播告知自己就可以了,大大减少了开发的工作量和开发周期。而作为应用开发者,就需要数练掌握Android系统提供的一个开发利器,那就是BroadcastReceiver。
在我们详细分析创建BroadcastReceiver的两种注册方式前,我们先罗列本次分析的大纲:
(1)对静态和动态两种注册方式进行概念阐述以及演示实现步骤
(2)简述两种BroadcastReceiver的类型(为后续注册方式的对比做准备)
(3)在默认广播类型下设置优先级和无优先级情况下两种注册方式的比较
(4)在有序广播类型下两种注册方式的比较
(5)通过接受打电话的广播,在程序(Activity)运行时和终止运行时,对两种注册方式的比较
(6)总结两种方式的特点
一、静态和动态注册方式
? 构建Intent,使用sendBroadcast方法发出广播定义一个广播接收器,该广播接收器继承BroadcastReceiver,并且覆盖onReceive()方法来响应事件注册该广播接收器,我们可以在代码中注册(动态注册),也可以AndroidManifest.xml配置文件中注册(静态注册)。

案例解析:

1.主界面设计

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btnSend"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:insetTop="16dp"
        android:text="发松" />
</LinearLayout>

如图:

在这里插入图片描述

2.后台代码设计

package com.aaa.btdemo02;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
    //定义对象;村长:一样权威,光辉的存在,拿着大喇叭,讲话;
    Button btnSend;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); //取值
        btnSend=(Button) findViewById(R.id.btnSend);
        //这对这个按钮做监听事件;发送信息,大喇叭...
        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                //设置intent的动作;后面字符串是自定义的
                intent.setAction("android.intent.action.receiverdata");
                intent.putExtra("msg","羊村各位村民开会了");
                MainActivity.this.sendBroadcast(intent);
            }
        });
    }
}

3.创建自己的广播接收器类

package com.aaa.btdemo02;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        //接受广播
        if(intent==null)return;
        //intent:接受从主端传递过来的数据,action数据;
        String action=intent.getAction();
        //针对上述做判断;第一个判断是否为空也可以写成action.isEmpty
        if(!TextUtils.isEmpty(action)&&"android.intent.action.receiverdata".equals(action)){
            String msg=intent.getStringExtra("msg");//不习惯可以使用Bundle
            Log.i("喜洋洋-->",msg);
        }
    }
}

4.注册广播

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.aaa.btdemo02">

    <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/Theme.Btdemo02">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".MyReceiver"
            android:exported="true">
            <intent-filter>
                <!-- 自定义的action名 -->
                <action android:name="android.intent.action.receiverdata"/>
            </intent-filter>
        </receiver>
    </application>

</manifest>

5.运行效果

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

到此这篇关于Android四大组件之广播BroadcastReceiver详解的文章就介绍到这了,更多相关Android 四大组件内容请搜索3672js教程以前的文章或继续浏览下面的相关文章希望大家以后多多支持3672js教程!

您可能感兴趣的文章:
  • Android编程四大组件之BroadcastReceiver(广播接收者)用法实例
  • Android BroadcastReceiver广播简单使用
  • Android BroadcastReceiver接收收到短信的广播
  • Android BroadcastReceiver广播注册方式总结
  • Android BroadcastReceiver广播机制概述
  • Android中BroadcastReceiver(异步接收广播Intent)的使用

用户评论