欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android四大組件之廣播BroadcastReceiver詳解

 更新時(shí)間:2021年11月04日 10:13:30   作者:tea_year  
Android開發(fā)的四大組件分別是:活動(dòng)(activity),用于表現(xiàn)功能;服務(wù)(service),后臺運(yùn)行服務(wù),不提供界面呈現(xiàn);廣播接受者(Broadcast Receive),勇于接收廣播;內(nèi)容提供者(Content Provider),支持多個(gè)應(yīng)用中存儲和讀取數(shù)據(jù),相當(dāng)于數(shù)據(jù)庫,本篇著重介紹廣播組件

定義

BroadcastReceiver,“廣播接收者”的意思,顧名思義,它就是用來接收來自系統(tǒng)和應(yīng)用中的廣播。在Android系統(tǒng)中,廣播體現(xiàn)在方方面面,例如當(dāng)開機(jī)完成后系統(tǒng)會產(chǎn)生一條廣播,接收到這條廣播就能實(shí)現(xiàn)開機(jī)啟動(dòng)服務(wù)的功能;當(dāng)網(wǎng)絡(luò)狀態(tài)改變時(shí)系統(tǒng)會產(chǎn)生一條廣播,接收到這條廣播就能及時(shí)地做出提示和保存數(shù)據(jù)等操作;當(dāng)電池電量改變時(shí),系統(tǒng)會產(chǎn)生一條廣播,接收到這條廣播就能在電量低時(shí)告知用戶及時(shí)保存進(jìn)度等等。Android中的廣播機(jī)制設(shè)計(jì)的非常出色,很多事情原本需要開發(fā)者親自操作的,現(xiàn)在只需等待廣播告知自己就可以了,大大減少了開發(fā)的工作量和開發(fā)周期。而作為應(yīng)用開發(fā)者,就需要數(shù)練掌握Android系統(tǒng)提供的一個(gè)開發(fā)利器,那就是BroadcastReceiver。
在我們詳細(xì)分析創(chuàng)建BroadcastReceiver的兩種注冊方式前,我們先羅列本次分析的大綱:
(1)對靜態(tài)和動(dòng)態(tài)兩種注冊方式進(jìn)行概念闡述以及演示實(shí)現(xiàn)步驟
(2)簡述兩種BroadcastReceiver的類型(為后續(xù)注冊方式的對比做準(zhǔn)備)
(3)在默認(rèn)廣播類型下設(shè)置優(yōu)先級和無優(yōu)先級情況下兩種注冊方式的比較
(4)在有序廣播類型下兩種注冊方式的比較
(5)通過接受打電話的廣播,在程序(Activity)運(yùn)行時(shí)和終止運(yùn)行時(shí),對兩種注冊方式的比較
(6)總結(jié)兩種方式的特點(diǎn)
一、靜態(tài)和動(dòng)態(tài)注冊方式
? 構(gòu)建Intent,使用sendBroadcast方法發(fā)出廣播定義一個(gè)廣播接收器,該廣播接收器繼承BroadcastReceiver,并且覆蓋onReceive()方法來響應(yīng)事件注冊該廣播接收器,我們可以在代碼中注冊(動(dòng)態(tài)注冊),也可以AndroidManifest.xml配置文件中注冊(靜態(tài)注冊)。

案例解析:

1.主界面設(shè)計(jì)

<?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="發(fā)松" />
</LinearLayout>

如圖:

在這里插入圖片描述

2.后臺代碼設(shè)計(jì)

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 {
    //定義對象;村長:一樣權(quán)威,光輝的存在,拿著大喇叭,講話;
    Button btnSend;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); //取值
        btnSend=(Button) findViewById(R.id.btnSend);
        //這對這個(gè)按鈕做監(jiān)聽事件;發(fā)送信息,大喇叭...
        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                //設(shè)置intent的動(dòng)作;后面字符串是自定義的
                intent.setAction("android.intent.action.receiverdata");
                intent.putExtra("msg","羊村各位村民開會了");
                MainActivity.this.sendBroadcast(intent);
            }
        });
    }
}

3.創(chuàng)建自己的廣播接收器類

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:接受從主端傳遞過來的數(shù)據(jù),action數(shù)據(jù);
        String action=intent.getAction();
        //針對上述做判斷;第一個(gè)判斷是否為空也可以寫成action.isEmpty
        if(!TextUtils.isEmpty(action)&&"android.intent.action.receiverdata".equals(action)){
            String msg=intent.getStringExtra("msg");//不習(xí)慣可以使用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.運(yùn)行效果

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

到此這篇關(guān)于Android四大組件之廣播BroadcastReceiver詳解的文章就介紹到這了,更多相關(guān)Android 四大組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論