获取andrid apn信息(转)

2024-01-10 08:38
文章标签 获取 信息 apn andrid

本文主要是介绍获取andrid apn信息(转),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本文转自  http://www.cnmsdn.com/html/201008/1282371656ID7414_2.html

 

 

 

由于Android对于APN的网络API没有公开,不过我们可以阅读源代码,然后进行数据库操作,系统会自动监听数据库的变化,从而实现开启或者关闭APN。

  大家可以研究一下frameworks/base/core/java/android/provider/Telephony.java这个类,

  比较重要的就是 URI 和数据库字段: content://telephony/carriers

  字段可以在Telephony.java中找到。

  其实原理很简单 :

  1 、 当开启APN的时候,设置一个正确的移动或者联通的APN

  2、 关闭的时候设置一个错误APN就会自动关闭网络

  请看代码:Activity:

package com.yuan;import java.util.ArrayList;
import java.util.List;import com.yuan.util.APNMatchTools;import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;public class ApnTest extends Activity {/** Called when the activity is first created. */Uri uri = Uri.parse("content://telephony/carriers");public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);Button open= (Button) findViewById(R.id.open);Button close= (Button) findViewById(R.id.close);open.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {openAPN();}});close.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {closeAPN();}});}public void openAPN(){List<APN> list = getAPNList();for (APN apn : list) {ContentValues cv = new ContentValues();cv.put("apn", APNMatchTools.matchAPN(apn.apn));cv.put("type", APNMatchTools.matchAPN(apn.type));getContentResolver().update(uri, cv, "_id=?", new String[]{apn.id});}}public void closeAPN(){List<APN> list = getAPNList();for (APN apn : list) {ContentValues cv = new ContentValues();cv.put("apn", APNMatchTools.matchAPN(apn.apn)+"mdev");cv.put("type", APNMatchTools.matchAPN(apn.type)+"mdev");getContentResolver().update(uri, cv, "_id=?", new String[]{apn.id});}}private List<APN> getAPNList(){String tag = "Main.getAPNList()";//current不为空表示可以使用的APNString projection[] = {"_id,apn,type,current"};Cursor cr = this.getContentResolver().query(uri, projection, null, null, null);List<APN> list = new ArrayList<APN>();while(cr!=null && cr.moveToNext()) {Log.d(tag, cr.getString(cr.getColumnIndex("_id")) + " " + cr.getString(cr.getColumnIndex("apn")) + " " + cr.getString(cr.getColumnIndex("type"))+ " " + cr.getString(cr.getColumnIndex("current")));APN a = new APN();a.id = cr.getString(cr.getColumnIndex("_id"));a.apn = cr.getString(cr.getColumnIndex("apn"));a.type = cr.getString(cr.getColumnIndex("type"));list.add(a);}if(cr!=null){cr.close();}return	list;}public static class APN{String id;String apn;String type;public String toString(){return this.id+":"+this.apn+":"+this.type;}}
}

 

package com.yuan.util;public class APNMatchTools {public static String matchAPN(String currentName) {if("".equals(currentName) || null==currentName){return "";}currentName = currentName.toLowerCase();if(currentName.startsWith(APNNet.CMNET))return APNNet.CMNET;else if(currentName.startsWith(APNNet.CMWAP))return APNNet.CMWAP;else if(currentName.startsWith(APNNet.GNET_3))return APNNet.GNET_3;else if(currentName.startsWith(APNNet.GWAP_3))return APNNet.GWAP_3;else if(currentName.startsWith(APNNet.UNINET))return APNNet.UNINET;else if(currentName.startsWith(APNNet.UNIWAP))return APNNet.UNIWAP;else if(currentName.startsWith("default"))return "default";else return "";}public static class APNNet{/*** 中国移动cmwap*/public static String CMWAP = "cmwap";/*** 中国移动cmnet*/public static String CMNET = "cmnet";//中国联通3GWAP设置 中国联通3G因特网设置 中国联通WAP设置 中国联通因特网设置//3gwap 3gnet uniwap uninet/*** 3G wap 中国联通3gwap APN*/public static String GWAP_3 = "3gwap";/*** 3G net 中国联通3gnet APN*/public static String GNET_3="3gnet";/*** uni wap 中国联通uni wap APN*/public static String UNIWAP="uniwap";/*** uni net 中国联通uni net APN*/public static String UNINET="uninet";}
}

 

 

  最后不要忘记加上修改APN的权限:

  Xml代码

<uses-permission android:name="android.permission.WRITE_APN_SETTINGS" />

   经过测试在G1 上联通和移动卡均是成功的。

推荐地址:http://www.cnmsdn.com/

 

这篇关于获取andrid apn信息(转)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/590196

相关文章

MySQL 获取字符串长度及注意事项

《MySQL获取字符串长度及注意事项》本文通过实例代码给大家介绍MySQL获取字符串长度及注意事项,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录mysql 获取字符串长度详解 核心长度函数对比⚠️ 六大关键注意事项1. 字符编码决定字节长度2

python3如何找到字典的下标index、获取list中指定元素的位置索引

《python3如何找到字典的下标index、获取list中指定元素的位置索引》:本文主要介绍python3如何找到字典的下标index、获取list中指定元素的位置索引问题,具有很好的参考价值,... 目录enumerate()找到字典的下标 index获取list中指定元素的位置索引总结enumerat

SpringMVC高效获取JavaBean对象指南

《SpringMVC高效获取JavaBean对象指南》SpringMVC通过数据绑定自动将请求参数映射到JavaBean,支持表单、URL及JSON数据,需用@ModelAttribute、@Requ... 目录Spring MVC 获取 JavaBean 对象指南核心机制:数据绑定实现步骤1. 定义 Ja

C++中RAII资源获取即初始化

《C++中RAII资源获取即初始化》RAII通过构造/析构自动管理资源生命周期,确保安全释放,本文就来介绍一下C++中的RAII技术及其应用,具有一定的参考价值,感兴趣的可以了解一下... 目录一、核心原理与机制二、标准库中的RAII实现三、自定义RAII类设计原则四、常见应用场景1. 内存管理2. 文件操

SpringBoot服务获取Pod当前IP的两种方案

《SpringBoot服务获取Pod当前IP的两种方案》在Kubernetes集群中,SpringBoot服务获取Pod当前IP的方案主要有两种,通过环境变量注入或通过Java代码动态获取网络接口IP... 目录方案一:通过 Kubernetes Downward API 注入环境变量原理步骤方案二:通过

使用Python实现获取屏幕像素颜色值

《使用Python实现获取屏幕像素颜色值》这篇文章主要为大家详细介绍了如何使用Python实现获取屏幕像素颜色值,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 一、一个小工具,按住F10键,颜色值会跟着显示。完整代码import tkinter as tkimport pyau

python获取cmd环境变量值的实现代码

《python获取cmd环境变量值的实现代码》:本文主要介绍在Python中获取命令行(cmd)环境变量的值,可以使用标准库中的os模块,需要的朋友可以参考下... 前言全局说明在执行py过程中,总要使用到系统环境变量一、说明1.1 环境:Windows 11 家庭版 24H2 26100.4061

Linux查看系统盘和SSD盘的容量、型号及挂载信息的方法

《Linux查看系统盘和SSD盘的容量、型号及挂载信息的方法》在Linux系统中,管理磁盘设备和分区是日常运维工作的重要部分,而lsblk命令是一个强大的工具,它用于列出系统中的块设备(blockde... 目录1. 查看所有磁盘的物理信息方法 1:使用 lsblk(推荐)方法 2:使用 fdisk -l(

使用Python获取JS加载的数据的多种实现方法

《使用Python获取JS加载的数据的多种实现方法》在当今的互联网时代,网页数据的动态加载已经成为一种常见的技术手段,许多现代网站通过JavaScript(JS)动态加载内容,这使得传统的静态网页爬取... 目录引言一、动态 网页与js加载数据的原理二、python爬取JS加载数据的方法(一)分析网络请求1

通过cmd获取网卡速率的代码

《通过cmd获取网卡速率的代码》今天从群里看到通过bat获取网卡速率两段代码,感觉还不错,学习bat的朋友可以参考一下... 1、本机有线网卡支持的最高速度:%v%@echo off & setlocal enabledelayedexpansionecho 代码开始echo 65001编码获取: >