android APN的打开与关闭.doc

上传人:11****ws 文档编号:3260029 上传时间:2019-05-27 格式:DOC 页数:6 大小:30.50KB
下载 相关 举报
android APN的打开与关闭.doc_第1页
第1页 / 共6页
android APN的打开与关闭.doc_第2页
第2页 / 共6页
android APN的打开与关闭.doc_第3页
第3页 / 共6页
android APN的打开与关闭.doc_第4页
第4页 / 共6页
android APN的打开与关闭.doc_第5页
第5页 / 共6页
点击查看更多>>
资源描述

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

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 .Uri;import android.os.Bundle;import android.util.Log;import android.view.View;import android.

3、widget.Button;public class Main extends Activity Uri uri = Uri.parse(“content:/telephony/carriers“);Overridepublic void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState);setContentView(R.layout.main);Button open= (Button) findViewById(R.id.open);Button close= (Button) findViewBy

4、Id(R.id.close);open.setOnClickListener(new View.OnClickListener() Overridepublic void onClick(View v) openAPN(););close.setOnClickListener(new View.OnClickListener() Overridepublic void onClick(View v) closeAPN(););public void openAPN()List list = getAPNList();for (APN apn : list) ContentValues cv =

5、 new ContentValues();cv.put(“apn“, APNMatchTools.matchAPN(apn.apn);cv.put(“type“, APNMatchTools.matchAPN(apn.type);getContentResolver().update(uri, cv, “_id=?“, new Stringapn.id);public void closeAPN()List list = getAPNList();for (APN apn : list) ContentValues cv = new ContentValues();cv.put(“apn“,

6、APNMatchTools.matchAPN(apn.apn)+“mdev“);cv.put(“type“, APNMatchTools.matchAPN(apn.type)+“mdev“);getContentResolver().update(uri, cv, “_id=?“, new Stringapn.id);private List getAPNList()String tag = “Main.getAPNList()“;/current 不为空表示可以使用的 APNString projection = “_id,apn,type,current“;Cursor cr = this

7、.getContentResolver().query(uri, projection, null, null, null);List list = new ArrayList();while(cr!=null 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();re

8、turn list;public static class APNString id;String apn;String type;APNMatchTools.javaJava 代码package cc.mdev.apn;public final class APNMatchTools public static class APNNetpublic static String CMWAP = “cmwap“;public static String CMNET = “cmnet“;/中国联通 3GWAP 设置 中国联通 3G 因特网设置 中国联通 WAP 设置 中国联通因特网设置/3gwap

9、 3gnet uniwap uninetpublic 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.toL

10、owerCase();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. 经过测试在 G1 上联通和移动卡均是成功的。

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 重点行业资料库 > 医药卫生

Copyright © 2018-2021 Wenke99.com All rights reserved

工信部备案号浙ICP备20026746号-2  

公安局备案号:浙公网安备33038302330469号

本站为C2C交文档易平台,即用户上传的文档直接卖给下载用户,本站只是网络服务中间平台,所有原创文档下载所得归上传人所有,若您发现上传作品侵犯了您的权利,请立刻联系网站客服并提供证据,平台将在3个工作日内予以改正。