Android通过代码打开和关闭网络连接
2013-05-10 08:37
513 查看
我们都知道,要打开网络连接是要有相应的权限的,但是目前Android中并没有直接打开和关闭网络连接的权限,因此我们只能通过APN来实现:
由于Android对于APN的网络API没有公开,不过我们可以阅读源代码,然后进行数据库操作,系统会自动监听数据库的变化,从而实现开启或者关闭APN。
其实原理很简单 :
1 、 当开启APN的时候,设置一个正确的移动或者联通的APN
2、 关闭的时候设置一个错误APN就会自动关闭网络
下面是源代码:
package com.mandr.network.demo;
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 OpenNetworkActivity extends Activity {
private Button open, close;
Uri uri =Uri.parse("content://telephony/carriers");
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
open = (Button)findViewById(R.id.open);
close = (Button)findViewById(R.id.close);
open.setOnClickListener(newView.OnClickListener() {
publicvoid onClick(View v) {
openAPN();
}
});
close.setOnClickListener(newView.OnClickListener() {
publicvoid onClick(View v) {
closeAPN();
}
});
}
public void openAPN() {
List list = getAPNList();
for (APN apn : list) {
ContentValuescv = new ContentValues();
cv.put("apn",APNMatchTools.matchAPN(apn.apn));
cv.put("type",APNMatchTools.matchAPN(apn.type));
getContentResolver().update(uri,cv, "_id=?",
newString[]
{ apn.id });
}
}
public void closeAPN() {
List list = getAPNList();
for (APN apn : list) {
ContentValuescv = new ContentValues();
cv.put("apn",APNMatchTools.matchAPN(apn.apn) + "mdev");
cv.put("type",APNMatchTools.matchAPN(apn.type) + "mdev");
getContentResolver().update(uri,cv, "_id=?",
newString[]
{ apn.id });
}
}
private List getAPNList() {
String tag ="Main.getAPNList()";
// current不为空表示可以使用的APN
String projection[] = {"_id,apn,type,current" };
//查询数据库
Cursor cr =this.getContentResolver().query(uri, projection, null,
null,null);
List list = newArrayList();
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 = newAPN();
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
package com.mandr.network.demo;
public class APNMatchTools {
public static class APNNet {
public static String CMWAP ="cmwap";
public static String CMNET ="cmnet";
// 中国联通3GWAP设置 中国联通3G因特网设置中国联通WAP设置 中国联通因特网设置
// 3gwap 3gnet uniwapuninet
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(StringcurrentName) {
if ("".equals(currentName) ||null == currentName) {
return"";
}
currentName =currentName.toLowerCase();
if(currentName.startsWith(APNNet.CMNET))
returnAPNNet.CMNET;
else if(currentName.startsWith(APNNet.CMWAP))
returnAPNNet.CMWAP;
else if(currentName.startsWith(APNNet.GNET_3))
returnAPNNet.GNET_3;
else if(currentName.startsWith(APNNet.GWAP_3))
returnAPNNet.GWAP_3;
else if(currentName.startsWith(APNNet.UNINET))
returnAPNNet.UNINET;
else if(currentName.startsWith(APNNet.UNIWAP))
returnAPNNet.UNIWAP;
else if(currentName.startsWith("default"))
return"default";
else
return"";
// returncurrentName.substring(0, currentName.length() -
// SUFFIX.length());
}
}
最后别忘了加入修改APN的权限:
由于Android对于APN的网络API没有公开,不过我们可以阅读源代码,然后进行数据库操作,系统会自动监听数据库的变化,从而实现开启或者关闭APN。
其实原理很简单 :
1 、 当开启APN的时候,设置一个正确的移动或者联通的APN
2、 关闭的时候设置一个错误APN就会自动关闭网络
下面是源代码:
package com.mandr.network.demo;
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 OpenNetworkActivity extends Activity {
private Button open, close;
Uri uri =Uri.parse("content://telephony/carriers");
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
open = (Button)findViewById(R.id.open);
close = (Button)findViewById(R.id.close);
open.setOnClickListener(newView.OnClickListener() {
publicvoid onClick(View v) {
openAPN();
}
});
close.setOnClickListener(newView.OnClickListener() {
publicvoid onClick(View v) {
closeAPN();
}
});
}
public void openAPN() {
List list = getAPNList();
for (APN apn : list) {
ContentValuescv = new ContentValues();
cv.put("apn",APNMatchTools.matchAPN(apn.apn));
cv.put("type",APNMatchTools.matchAPN(apn.type));
getContentResolver().update(uri,cv, "_id=?",
newString[]
{ apn.id });
}
}
public void closeAPN() {
List list = getAPNList();
for (APN apn : list) {
ContentValuescv = new ContentValues();
cv.put("apn",APNMatchTools.matchAPN(apn.apn) + "mdev");
cv.put("type",APNMatchTools.matchAPN(apn.type) + "mdev");
getContentResolver().update(uri,cv, "_id=?",
newString[]
{ apn.id });
}
}
private List getAPNList() {
String tag ="Main.getAPNList()";
// current不为空表示可以使用的APN
String projection[] = {"_id,apn,type,current" };
//查询数据库
Cursor cr =this.getContentResolver().query(uri, projection, null,
null,null);
List list = newArrayList();
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 = newAPN();
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
package com.mandr.network.demo;
public class APNMatchTools {
public static class APNNet {
public static String CMWAP ="cmwap";
public static String CMNET ="cmnet";
// 中国联通3GWAP设置 中国联通3G因特网设置中国联通WAP设置 中国联通因特网设置
// 3gwap 3gnet uniwapuninet
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(StringcurrentName) {
if ("".equals(currentName) ||null == currentName) {
return"";
}
currentName =currentName.toLowerCase();
if(currentName.startsWith(APNNet.CMNET))
returnAPNNet.CMNET;
else if(currentName.startsWith(APNNet.CMWAP))
returnAPNNet.CMWAP;
else if(currentName.startsWith(APNNet.GNET_3))
returnAPNNet.GNET_3;
else if(currentName.startsWith(APNNet.GWAP_3))
returnAPNNet.GWAP_3;
else if(currentName.startsWith(APNNet.UNINET))
returnAPNNet.UNINET;
else if(currentName.startsWith(APNNet.UNIWAP))
returnAPNNet.UNIWAP;
else if(currentName.startsWith("default"))
return"default";
else
return"";
// returncurrentName.substring(0, currentName.length() -
// SUFFIX.length());
}
}
最后别忘了加入修改APN的权限:
相关文章推荐
- Android 监听 Android中监听系统网络连接打开或者关闭的实现代码
- Android中监听系统网络连接打开或者关闭的实现代码
- android中如何通过代码来开启和关闭移动网络
- [Android] 监听系统网络连接打开或者关闭的消息
- Android 通过反射打开与关闭数据连接
- Android 通过代码设置、打开wifi热点及热点连接的实现代码
- Android基于配置文件的测试模式(不修改代码通过配置文件来打开和关闭测试模式)
- 浅析android下如何通过jni监控wifi网络连接、dhcpcd执行和power电源控制
- android 开发中判断网络是否连接的代码
- iPhone锁屏网络连接关闭问题(iphone锁屏打开时请求网络失败)
- 【C#】Windows Mobile 通过代码设置GPRS连接,设置网络与接入点
- 手把手教你用Android手机通过电脑连接网络
- Android判断网络连接是否可用,WiFi、移动数据是否打开等
- android 建立蓝牙共享网络后,数据连接图标被打开
- android下如何通过jni监控wifi网络连接、dhcpcd执行和power电源控制
- Android通过广播判断网络连接(仿QQ网络连接显示)
- android 代码设置、打开wifi热点及热点的连接(转)
- Android打开WIFI或者移动网络的代码实现
- Android通过APN进行网络连接
- android使用代码实现gps,gprs,wifi打开与关闭