由于Android对于APN的网络API没有公开,不过我们可以阅读源代码,然后进行数
据库操作,系统会自动监听数据库的变化,从而实现开启或者关闭APN。
大家可以研究一下frameworks/base/core/java/android/provider
/Telephony.java这个类,
比较重要的就是 URI 和数据库字段: content://telephony/carriers
字段可以在Telephony.java中找到。
其实原理很简单 :
1 、 当开启APN的时候,设置一个正确的移动或者联通的APN
2、 关闭的时候设置一个错误APN就会自动关闭网络
看代码:Activity:

Java代码
package cc.mdev.apn;
import java.util.ArrayList;
import java.util.List;
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 Main extends Activity {

Uri uri = Uri.parse("content://telephony/carriers");
@Override
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() {
@Override
public void onClick(View v) {
openAPN();
}
});
close.setOnClickListener(new View.OnClickListener() {
@Override
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不为空表示可以使用的APN
String  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;
}
}

APNMatchTools.java
Java代码
package cc.mdev.apn;

public final class APNMatchTools {
public static class APNNet{

public static String CMWAP = "cmwap";

public static String CMNET = "cmnet";
//中国联通3GWAP设置        中国联通3G因特网设置        中国联通WAP设置        中国联通因特网设置
//3gwap                 3gnet                uniwap            uninet

public static String GWAP_3 = "3gwap";

public static String GNET_3="3gnet";

public static String UNIWAP="uniwap";

public static String UNINET="uninet";
}
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 "";
// return currentName.substring(0, currentName.length() - SUFFIX.length());
}
}

最后不要忘记加上修改APN的权限:
Xml代码
   1. <uses-permission android:name="android.permission.WRITE_APN_SETTINGS"></uses-permission> 
<uses-permission android:name="android.permission.WRITE_APN_SETTINGS"></uses-permission>
 经过测试在G1 上联通和移动卡均是成功的。

转载于:https://www.cnblogs.com/rollrock/archive/2011/07/04/2097193.html

android APN的打开与关闭相关推荐

  1. Android 蓝牙 单独打开和关闭BLE - 详解

    同学,别退出呀,我可是全网最牛逼的 Android 蓝牙分析博主,我写了上百篇蓝牙文章,请点击下面了解本专栏,进入本博主主页看看再走呗,一定不会让你后悔的,记得一定要去看主页置顶文章哦. 1.常规操作 ...

  2. android 打开闪光灯,Android 闪光灯的打开和关闭

    网上的方法大部分只支持 6.0 以下的手机,于是写了个兼容 6.0 以上手机的工具类: class FlashUtils { companion object { fun get():FlashUti ...

  3. android关闭传感器,您如何在安卓10手机上打开和关闭传感器

    这是您如何在Android 10手机上打开和关闭传感器的方法.最新的Android智能手机配备了各种传感器和突破性的新功能.温度,湿度和气压计传感器,接近传感器,RGB光传感器,加速度计,磁力计和陀螺 ...

  4. 【转】android软键盘显示与主窗口调整方式设置,Android软键盘显示模式及打开和关闭方式(推荐)...

    Android软键盘显示模式: Android定义了一个属性,名字为windowSoftInputMode,用它可以让程序可以控制活动主窗口调整的方式.我们可以在AndroidManifet.xml中 ...

  5. Android APN的设置问题 默认“已起用数据” 关闭

    说明: (1),参考:http://myqdroid.blog.51cto.com/2057579/389134 (2),应用的到程序 android2.3.4_GB_T34H\build\core\ ...

  6. android 弹出编辑框,Android编程实现的EditText弹出打开和关闭工具类

    本文实例讲述了Android编程实现的EditText弹出打开和关闭工具类.分享给大家供大家参考,具体如下: 需求: 使用代码实现Android的输入框EditText对键盘的关闭弹出的实现. 代码: ...

  7. Android 监听 Android中监听系统网络连接打开或者关闭的实现代码

    本篇文章对Android中监听系统网络连接打开或者关闭的实现用实例进行了介绍.需要的朋友参考下 很简单,所以直接看代码 复制代码 代码如下: package xxx; import android.c ...

  8. android打开sqlite数据库,Android:打开和关闭SQLite数据库

    我正在开发和android应用程序,我经常使用它访问本地数据库.这个数据库可以从不同的therads访问,所以我对数据库有一个协调问题.我使用以下open()和close()方法.Android:打开 ...

  9. android adb命令唤醒屏幕,如何使用adb命令打开和关闭屏幕?

    我正在使用KEYCODE_POWER来打开和关闭我的手机.波纹管命令用于打开和关闭屏幕.如何使用adb命令打开和关闭屏幕? adb shell input keyevent KEYCODE_POWER ...

  10. Android蓝牙开发之一:打开、关闭蓝牙

    转载地址:http://victorzhong.github.io/2015/07/26/Android%E8%93%9D%E7%89%99%E5%BC%80%E5%8F%91%E4%B9%8B%E4 ...

最新文章

  1. C++中访问类的私有数据成员的第三种方法
  2. 这样建统一告警平台,运维的告警麻痹症有救了
  3. 11月3号晚7点中科院南土所梁玉婷报告:土壤微生物功能组学与关键类群识别
  4. PMP考前复习题 系列三
  5. virtualenv在ubuntu系统中的缺点
  6. 【AI独角兽招聘】这里有一个梦,我们一同前往…
  7. 你真的了解JS数组的那些方法吗?
  8. 强大的代码编辑工具:Nova for mac v7.3中文版
  9. 支付宝-沙箱环境配置和使用
  10. 智能美容仪APP开发作用特点
  11. 牛客网题源(JavaScript)
  12. 数据挖掘神经网络算法,人工神经网络分析方法
  13. 夜神模拟器的安装和使用
  14. Linux ln -s目录,Linux 中的 ln 命令
  15. FusionCharts 参数设置
  16. 【Python随笔】python进程池ProcessPoolExecutor的用法与实现分析
  17. 平安科技面试(成都应届)
  18. 如何优雅地使用Sublime
  19. 系统特征根_20160204
  20. 易订宝 移动营销网络订货平台

热门文章

  1. MFC在一个工程中启动其他工程的exe文件
  2. WPF学习笔记(5):两个DataGrid的滚动条实现同步滚动(转)
  3. python Image 模块处理图片
  4. 21模块-orientation【管理设备的方向信息】
  5. 在python 中is和= = 的区别
  6. It#39;s about trust
  7. Extjs Ext.net中的常用属性
  8. 2016年9月ccf
  9. event.type 事件属性
  10. [Project Euler] Problem 48