Android实现APN设置专网并切换默认网络

news/2025/2/6 19:32:08/

*最近接触到了银联Pos通这些业务,需要使用专网进行数据报文通讯,在这里自己写了一个工具类,进行配置APN专网
参数,并进行默认切换到自己添加进去的APN,实现专网数据通讯。
注意:为了防止在本地重复添加同一个APN数据,在每次保存之前先进行判断,没有检查到对应专网名的数据时进行本地添加否则直接进行专网切换即可。*


下面贴出工具类源代码:

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.net.Uri;
import android.telephony.TelephonyManager;
import android.util.Log;/*** Created by hizha on 2018/4/17.*/public class APN {//取得全部的APN列表:public static final Uri APN_URI = Uri.parse("content://telephony/carriers");//    取得当前设置的APN:public static final Uri preferapn = Uri.parse("content://telephony/carriers/preferapn");//取得current=1的APNpublic static final Uri CURRENT_APN_URI = Uri.parse("content://telephony/carriers/current");private Context context;public static boolean hasAPN;public APN(Context context){this.context = context;}// 新增一个cmnet接入点//    public void APN() {////        checkAPN();//    }public int addAPN() {int id = -1;String NUMERIC = getSIMInfo();if (NUMERIC == null) {return -1;}ContentResolver resolver = context.getContentResolver();ContentValues values = new ContentValues();values.put("name", "专用APN");                                  //apn中文描述values.put("apn", "xxx.ln");                                     //apn名称values.put("type", "default");                            //apn类型values.put("numeric", NUMERIC);values.put("mcc", NUMERIC.substring(0, 3));values.put("mnc", NUMERIC.substring(3, NUMERIC.length()));values.put("proxy", "");                                        //代理values.put("port", "1116");                                         //端口values.put("mmsproxy", "");                                     //彩信代理values.put("mmsport", "");                                      //彩信端口values.put("user", "");                                         //用户名values.put("server","1.1.1.1");                                       //服务器values.put("password", "");                                     //密码values.put("mmsc", "");                                          //MMSCCursor c = null;Uri newRow = resolver.insert(APN_URI, values);if (newRow != null) {c = resolver.query(newRow, null, null, null, null);int idIndex = c.getColumnIndex("_id");c.moveToFirst();id = c.getShort(idIndex);}if (c != null)c.close();return id;}protected String getSIMInfo() {TelephonyManager iPhoneManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);return iPhoneManager.getSimOperator();}// 设置接入点public void SetAPN(int id) {ContentResolver resolver = context.getContentResolver();ContentValues values = new ContentValues();values.put("apn_id", id);resolver.update(APN_URI, values, null, null);
//        resolver.update(CURRENT_APN_URI, values, null, null);}public void checkAPN() {// 检查当前连接的APNCursor cr = context.getContentResolver().query(APN_URI, null, null, null, null);while (cr != null && cr.moveToNext()) {if (cr.getString(cr.getColumnIndex("apn")).equals("dlums.ln")) {Log.d("TAG", "apn: " + cr.getString(cr.getColumnIndex("apn")));int apnId = cr.getShort(cr.getColumnIndex("_id"));APN.hasAPN = true;Log.d("TAG", "id: " +apnId);setDefaultApn(apnId);break;}}}/*** 根据apnId将设置的APN选中** @param apnId* @return*/public boolean setDefaultApn(int apnId) {boolean res = false;Log.d("TAG","setDefaultApn:" + apnId);ContentResolver resolver = context.getContentResolver();ContentValues values = new ContentValues();values.put("apn_id", apnId);try {resolver.update(preferapn, values, null, null);Cursor c = resolver.query(preferapn, new String[]{"name","apn"}, "_id=" + apnId, null, null);if (c != null) {res = true;c.close();}} catch (SQLException e) {e.printStackTrace();}return res;}
}

使用的时候直接如下:

      APN apn = new APN(this);apn.checkAPN();if (!APN.hasAPN) {apn.SetAPN(apn.addAPN());}

http://www.ppmy.cn/news/670251.html

相关文章

android应用apn.xml,android之APN

APN全称是Access Point Name,中文即接入点,是通过手机上网时必须配置的一个参数,它决定了手机通过哪种接入方式来访问网络。 android系统把所有的APN都保存在数据库中,数据库绝对路径:/data/data/com.android.providers.telephony/databases/telephony.db。 使用adb命令可…

iPhone,iOS 12 APN设置(手机数据模式,蜂窝网络使用代理教程)

写在前面的话: 我的目的就是为了设置代理,但之前必须连接WIFI才能设置代理,我就想通过手机卡上网怎么设置代理呢? 还真有这个功能:APN。 安卓的教程有人已经写过了,直接照着弄就好:https://www.jianshu.com/p/1972c5ee4d43(感谢原创作者供图) iOS用户直接跳过下图。重点…

三方app跳转APN设置提示“此用户无权修改接入点名称设置”解决方案

三方APP跳转设置APN时,发现会提示如下 开始还以为是系统本身的问题,从网上搜索也看到很多人在问这个问题,但是从设置菜单点击进入的却没有“无权修改”的这个提示,看到这就放心多了。于是确定跟踪设置apn的这部分代码 com.androi…

ios修改apn的插件_GitHub - SuPair/APN: [已废弃]适合 iOS 7 的 APN 设置工具,又一个没过审核的周末项目...

为啥没过审核? 这个 App 没有违反任何 guideline,但是审核的人说苹果已经不允许设置 APN 的 App 上架了。App Store 上还有一些没被下架,这不代表新的可以上架。 那就开源出来吧,会用 APN 的小伙伴身边一定有一个有开发帐号的小伙…

网速变慢、不稳定?请自查一下,物联卡apn设置你是这样设置的吗

物联卡网速不好怎么破解限速,众所周知,物联卡用在手机上会经常出现网络慢、限速的情况,那么,如果出现信号该如何解决呢? 一,首先,先建议你自查一下是否属于以下几种情况 1、终端设备&#xff08…

apn服务器源码,android 设置apn

【实例简介】 android 设置apn,包括自动和手动,成熟代码 【实例截图】 【核心代码】 MyApnDemo └── MyApnDemo ├── AndroidManifest.xml ├── bin │ ├── AndroidManifest.xml │ └── classes │ └── com │ └── apn │ ├…

Android APN设置接口

APN(Access Point Name),即“接入点名称”,用来标识GPRS的业务种类,目前分为两大类:CMWAP(通过GPRS访问WAP业务)、CMNET(除了WAP以外的服务目前都用CMNET,比如连接因特网等)。在做项目的时候,客户会要求更换…

物联网卡设置_聆听物联物联网卡APN设置教程

APN只有移动流量卡需要设置,电信基本不用设置 APN如何设置? 手机设置-网络设置-找到本卡-APN设置-新建APN 具体位置请自行查找 172开头移动卡APN参数: 设置-->更多的网络-->移动网络-->接入点名称 -->添加新的接入点 名称:聆听物联 APN:CMIOTLVQ.js 代理:(空…