【Android 系统开发】_“四大组件”篇 -- “动态广播” 和 “静态广播”

发布时间:2019-08-06 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了【Android 系统开发】_“四大组件”篇 -- “动态广播” 和 “静态广播”脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

动态注册

Code

新建一个 broadcasttest 项目,修改 MainActivITy :

package com.example.marco.broadcasttest;

import andROId.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    PRivate IntentFilter intentFilter;

    private NetworkChangeReceiver networkChangeReceiver;

    @override
    protected void onCreate(Bundle savedInstancestate) {
        suPEr.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        intentFilter = new IntentFilter();
        intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        networkChangeReceiver = new NetworkChangeReceiver();
        registerReceiver(networkChangeReceiver, intentFilter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(networkChangeReceiver);
    }


    private class NetworkChangeReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context, "network changes", Toast.LENGTH_LONG).show();
        }
    }
}

静态注册

“动态注册”的广播接收器可以自由地控制注册与注销,在灵活性方面有很大的优势,但是它也存在一个缺点:必须要在程序启动之后才能接收到广播,因为注册的逻辑是写在 onCreate() 方法中的。

那么有没有什么方法可以让程序在未启动的情况下就能接收到广播呢?这就是我们即将看到的“静态注册”方法了!

Code

(1)新建一个 BroadcastReceiver:

package com.example.marco.broadcasttest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class BootcompleteReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Boot Complete", Toast.LENGTH_LONG).show();
    }
}

(2)静态广播接收器一定要在 AndroidManifest.XMl 文件中注册才可以使用,如果你使用 Android Studio 快捷方式创建了这个广播接收器,那么系统会自动在 AndroidManifest.xML 中注册好这个广播接收器:

<?xml version="1.0" encoding="utf-8"?>
<manifest >"http://schemas.android.COM/apk/res/android"
    package="com.example.marco.broadcasttest">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_@R_360_502@"
        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=".BootCompleteReceiver"
            android:enabled="true"                  // 是否启用这个广播接收器
            android:exported="true"></receiver>     // 是否允许这个广播接收器接收本程序以外的广播
    </application>

</manifest>

(3)此时广播接收器还未能接收到开机广播,需要添加 Action 和 权限:

<?xml version="1.0" encoding="utf-8"?>
<manifest >"http://schemas.android.com/apk/res/android"
    package="com.example.marco.broadcasttest">

    // 添加权限
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <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=".BootCompleteReceiver"
            android:enabled="true"
            android:exported="true">
            // 添加相应的 Action
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

脚本宝典总结

以上是脚本宝典为你收集整理的【Android 系统开发】_“四大组件”篇 -- “动态广播” 和 “静态广播”全部内容,希望文章能够帮你解决【Android 系统开发】_“四大组件”篇 -- “动态广播” 和 “静态广播”所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。