当前位置: 首页 > news >正文

【开发方案】Android 应用双卡搜网功能

一、功能简介

需求:开机自动开始搜网并显示网络列表

那么就不能将相关类做成单例,不能将subId、phoneId等卡相关的属性作为UI、服务的全局变量。

二、流程设计

NetworkSelectReceiver:监听开机广播,触发拉起搜网服务

NetworkOperatorService:搜网服务,完成后调起用户界面

NetworkOperatorList:网络模式列表,显示搜网结果

三、开发代码

(一)NetworkSelectReceiver

接收器配置 AndroidManifest.xml

        <receiverandroid:name=".settings.network.NetworkSelectReceiver"android:exported="true"><intent-filter><action android:name="android.intent.action.BOOT_COMPLETED" /><action android:name="android.intent.action.SIM_STATE_CHANGED" /></intent-filter></receiver>

内部逻辑

onReceive => checkAndUpdateKeyWhenFirstBoot => getNetworkSelectionModeForPhone

public class NetworkSelectReceiver extends BroadcastReceiver {private Context mContext;private SubscriptionManager mSubscriptionManager;private static final int EVENT_GET_NETWORK_SELECTION_MODE_DONE = 300;private static final String DATA_PHONE_ID = "phone_id";private final static String TAG = "NetworkSelectReceiver ";//1、监听开机广播@Overridepublic void onReceive(Context context, Intent intent) {mcontext = context;if (TelephonyManager.getDefault().isMultiSimEnabled()) { //双卡设备mSubscriptionManager = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);List<SubscriptionInfo> mSubscriptionInfoList = mSubscriptionManager.getActiveSubscriptionInfoList();int phoneCount = TelephonyManager.getDefault().getPhoneCount();if (mSubscriptionInfoList == null) {Log.d(TAG, "onReceive, no ActiveSubscriptionInfoList, phoneCount = " + phoneCount);return ;} else {Log.d(TAG, "onReceive, mSubscriptionInfoList = " + mSubscriptionInfoList + ", phoneCount = " + phoneCount);}//遍历并操作每一张卡。for (int i = 0; i < phoneCount; i++) {if (SubscriptionManager.isValidPhoneId(i)) {Log.d(TAG,"onReceive, isValidPhoneId = " + i);checkAndUpdateKeyWhenFirstBoot(context, i);}}} else {}//单卡逻辑}  //onReceive//2、场景校验(根据需求添加)private boolean checkAndUpdateKeyWhenFirstBoot(Context context, int phoneId) {Log.d(TAG,"checkAndUpdateKeyWhenFirstBoot, phoneId = " + phoneId);SharedPreferences prefs = ShowNetworkUtils.getSharedPreferences(context);String simKey = NetworkUtils.KEY_FIRST_BOOT + phoneId;//没有用到卡信息,此处用于debug,确认双卡是否正常TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);String mccmnc = "";if (tm == null) {return false;} else {mccmnc = tm.getSimOperatorNumericForPhone(phoneId);}int simState = tm.getSimState(phoneId);Log.d(TAG,"checkAndUpdateKeyWhenFirstBoot, simState = " + simState + ", mccmnc =" + mccmnc);//卡没加载好以及飞行模式下不发起搜网boolean isEnabled = (Settings.Glob
http://www.lryc.cn/news/324415.html

相关文章:

  • 图论基础|深度优先dfs、广度优先bfs
  • Python从入门到精通秘籍十七
  • Java——抽象类和接口
  • React—— props校验(非typescript校验类型)
  • Go——map操作及原理
  • 网络安全实训Day9
  • kubernetes实战(1)之虚拟机centos搭建k8s集群
  • 基于python+vue分类信息服务平台移动端的设计与实现flask-django-php-nodejs
  • 【蓝牙协议栈】【BLE】低功耗蓝牙配对绑定过程分析(超详细)
  • MySQL表内容的增删查改
  • Java的三大特性之一——多态(完)
  • 算法-最短路径
  • 【软考---系统架构设计师】特殊的操作系统介绍
  • 大模型: 提示词工程(prompt engineering)
  • RabbitMQ的事务机制
  • 41 物体检测和目标检测数据集【李沐动手学深度学习v2课程笔记】
  • 软件包管理(rpm+yum)
  • 网关层针对各微服务动态修改Ribbon路由策略
  • 如何从零开始拆解uni-app开发的vue项目(二)
  • 【生成对抗网络GAN】一篇文章讲透~
  • 【设计模式】Java 设计模式之模板命令模式(Command)
  • 如何在Flutter中实现一键登录
  • Amazon SageMaker + Stable Diffusion 搭建文本生成图像模型
  • FPGA数字信号处理前沿
  • 【Android】系统启动流程分析 —— init 进程启动过程
  • 抖音视频批量下载软件可导出视频分享链接|手机网页视频提取|视频爬虫采集工具
  • 鸿蒙Harmony应用开发—ArkTS-@Observed装饰器和@ObjectLink装饰器:嵌套类对象属性变化
  • 深度解析:Elasticsearch写入请求处理流程
  • 数据结构:堆和二叉树遍历
  • [Halcon学习笔记]在Qt上实现Halcon窗口的字体设置颜色设置等功能