app开发快速理解——广播——Broadcast——分钟广播&接收网络变更广播示例

 

 

package com.example.chapter09;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.chapter09.util.DateUtil;

public class SystemMinuteActivity extends AppCompatActivity {
    private TextView tv_minute; // 声明一个文本视图对象
    private String desc = "开始侦听分钟广播,请稍等。注意要保持屏幕亮着,才能正常收到广播";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_system_minute);
        tv_minute = findViewById(R.id.tv_minute);
        tv_minute.setText(desc);
    }

    @Override
    protected void onStart() {
        super.onStart();
        timeReceiver = new TimeReceiver(); // 创建一个分钟变更的广播接收器
        // 创建一个意图过滤器,只处理系统分钟变化的广播
        IntentFilter filter = new IntentFilter(Intent.ACTION_TIME_TICK);
        registerReceiver(timeReceiver, filter); // 注册接收器,注册之后才能正常接收广播
    }

    @Override
    protected void onStop() {
        super.onStop();
        unregisterReceiver(timeReceiver); // 注销接收器,注销之后就不再接收广播
    }

    private TimeReceiver timeReceiver; // 声明一个分钟广播的接收器实例
    // 定义一个分钟广播的接收器
    private class TimeReceiver extends BroadcastReceiver {
        // 一旦接收到分钟变更的广播,马上触发接收器的onReceive方法
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null) {
                desc = String.format("%s\n%s 收到一个分钟到达广播%s", desc,
                        DateUtil.getNowTime(), intent.getAction());
                tv_minute.setText(desc);
            }
        }
    }

}

  

 

 

package com.example.chapter09;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.chapter09.util.DateUtil;
import com.example.chapter09.util.NetworkUtil;

public class SystemNetworkActivity extends AppCompatActivity {
    private TextView tv_network; // 声明一个文本视图对象
    private String desc = "开始侦听网络变更广播,请开关WLAN或者数据连接,再观察广播结果";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_system_network);
        tv_network = findViewById(R.id.tv_network);
        tv_network.setText(desc);
    }

    @Override
    protected void onStart() {
        super.onStart();
        networkReceiver = new NetworkReceiver(); // 创建一个网络变更的广播接收器
        // 创建一个意图过滤器,只处理网络状态变化的广播
        IntentFilter filter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
        registerReceiver(networkReceiver, filter); // 注册接收器,注册之后才能正常接收广播
    }

    @Override
    protected void onStop() {
        super.onStop();
        unregisterReceiver(networkReceiver); // 注销接收器,注销之后就不再接收广播
    }

    private NetworkReceiver networkReceiver; // 声明一个网络变更的广播接收器实例
    // 定义一个网络变更的广播接收器
    private class NetworkReceiver extends BroadcastReceiver {
        // 一旦接收到网络变更的广播,马上触发接收器的onReceive方法
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null) {
                NetworkInfo networkInfo = intent.getParcelableExtra("networkInfo");
                String networkClass = NetworkUtil.getNetworkClass(networkInfo.getSubtype());
                desc = String.format("%s\n%s 收到一个网络变更广播,网络大类为%s," +
                                "网络小类为%s,网络制式为%s,网络状态为%s",
                        desc, DateUtil.getNowTime(), networkInfo.getTypeName(),
                        networkInfo.getSubtypeName(), networkClass,
                        networkInfo.getState().toString());
                tv_network.setText(desc);
            }
        }
    }

}

  

 

 

package com.example.chapter09.util;

import android.telephony.TelephonyManager;

public class NetworkUtil {
    // 获取数据连接的制式类型
    public static String getNetworkClass(int subType) {
        switch (subType) {
            case TelephonyManager.NETWORK_TYPE_GPRS:
            case TelephonyManager.NETWORK_TYPE_EDGE:
            case TelephonyManager.NETWORK_TYPE_CDMA:
            case TelephonyManager.NETWORK_TYPE_1xRTT:
            case TelephonyManager.NETWORK_TYPE_IDEN:
                return "2G";
            case TelephonyManager.NETWORK_TYPE_UMTS:
            case TelephonyManager.NETWORK_TYPE_EVDO_0:
            case TelephonyManager.NETWORK_TYPE_EVDO_A:
            case TelephonyManager.NETWORK_TYPE_HSDPA:
            case TelephonyManager.NETWORK_TYPE_HSUPA:
            case TelephonyManager.NETWORK_TYPE_HSPA:
            case TelephonyManager.NETWORK_TYPE_EVDO_B:
            case TelephonyManager.NETWORK_TYPE_EHRPD:
            case TelephonyManager.NETWORK_TYPE_HSPAP:
                return "3G";
            case TelephonyManager.NETWORK_TYPE_LTE:
                return "4G";
            case TelephonyManager.NETWORK_TYPE_NR:
                return "5G";
            default:
                return "未知";
        }
    }
}

  

 

posted @ 2021-12-26 18:54  小白龙白龙马  阅读(143)  评论(0编辑  收藏  举报