获取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

相关文章

python获取指定名字的程序的文件路径的两种方法

《python获取指定名字的程序的文件路径的两种方法》本文主要介绍了python获取指定名字的程序的文件路径的两种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 最近在做项目,需要用到给定一个程序名字就可以自动获取到这个程序在Windows系统下的绝对路径,以下

SpringBoot 获取请求参数的常用注解及用法

《SpringBoot获取请求参数的常用注解及用法》SpringBoot通过@RequestParam、@PathVariable等注解支持从HTTP请求中获取参数,涵盖查询、路径、请求体、头、C... 目录SpringBoot 提供了多种注解来方便地从 HTTP 请求中获取参数以下是主要的注解及其用法:1

SQL Server跟踪自动统计信息更新实战指南

《SQLServer跟踪自动统计信息更新实战指南》本文详解SQLServer自动统计信息更新的跟踪方法,推荐使用扩展事件实时捕获更新操作及详细信息,同时结合系统视图快速检查统计信息状态,重点强调修... 目录SQL Server 如何跟踪自动统计信息更新:深入解析与实战指南 核心跟踪方法1️⃣ 利用系统目录

Python获取浏览器Cookies的四种方式小结

《Python获取浏览器Cookies的四种方式小结》在进行Web应用程序测试和开发时,获取浏览器Cookies是一项重要任务,本文我们介绍四种用Python获取浏览器Cookies的方式,具有一定的... 目录什么是 Cookie?1.使用Selenium库获取浏览器Cookies2.使用浏览器开发者工具

Java获取当前时间String类型和Date类型方式

《Java获取当前时间String类型和Date类型方式》:本文主要介绍Java获取当前时间String类型和Date类型方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录Java获取当前时间String和Date类型String类型和Date类型输出结果总结Java获取

C#监听txt文档获取新数据方式

《C#监听txt文档获取新数据方式》文章介绍通过监听txt文件获取最新数据,并实现开机自启动、禁用窗口关闭按钮、阻止Ctrl+C中断及防止程序退出等功能,代码整合于主函数中,供参考学习... 目录前言一、监听txt文档增加数据二、其他功能1. 设置开机自启动2. 禁止控制台窗口关闭按钮3. 阻止Ctrl +

一文详解如何使用Java获取PDF页面信息

《一文详解如何使用Java获取PDF页面信息》了解PDF页面属性是我们在处理文档、内容提取、打印设置或页面重组等任务时不可或缺的一环,下面我们就来看看如何使用Java语言获取这些信息吧... 目录引言一、安装和引入PDF处理库引入依赖二、获取 PDF 页数三、获取页面尺寸(宽高)四、获取页面旋转角度五、判断

Java中读取YAML文件配置信息常见问题及解决方法

《Java中读取YAML文件配置信息常见问题及解决方法》:本文主要介绍Java中读取YAML文件配置信息常见问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录1 使用Spring Boot的@ConfigurationProperties2. 使用@Valu

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

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

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