Android中GPRS 开关
package com.cb;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.util.Log;
/**
*
* @author CB 2012.05.23
*/
public class GPRSManager {
final Uri MAIN_APN = Uri.parse("content://telephony/carriers/preferapn");
final Uri ALL_APN = Uri.parse("content://telephony/carriers");
private Context cx;
public GPRSManager(Context cx) {
// TODO Auto-generated constructor stub
this.cx = cx;
}
private static GPRSManager instance = null;
public static synchronized GPRSManager getInstance(Context cx) {
if (instance == null) {
instance = new GPRSManager(cx);
}
return instance;
}
public boolean isNetworkAvailable() {
ConnectivityManager mConnMgr = (ConnectivityManager) cx
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = mConnMgr
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mMobile = mConnMgr
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
boolean flag = false;
if ((mWifi != null)
&& ((mWifi.isAvailable()) || (mMobile.isAvailable()))) {
if ((mWifi.isConnected()) || (mMobile.isConnected())) {
flag = true;
}
}
return flag;
}
private List<APN> getAPNList() throws Exception {
String tag = "getAPNList()";
Cursor cr = cx.getContentResolver().query(MAIN_APN,
new String[] { "_id" }, null, null, null);
String curApnId = null;
if (cr != null && cr.moveToFirst()) {
curApnId = cr.getString(cr.getColumnIndex("_id"));
}
cr.close();
// ------------------------------------------
String projection[] = { "_id,apn,type,current" };
cr = cx.getContentResolver().query(ALL_APN, projection, "_id = ?",
new String[] { curApnId }, 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 void openAPN() throws Exception {
toggleMobileData(cx, true);
try {
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));
cx.getContentResolver().update(ALL_APN, cv, "_id=?",
new String[] { apn.id });
}
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
}
public void closeAPN() throws Exception {
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");
cx.getContentResolver().update(ALL_APN, cv, "_id=?",
new String[] { apn.id });
}
}
public class APN {
String id;
String apn;
String type;
}
public boolean getMobileDataStatus(Context context) {
ConnectivityManager conMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
Class<?> conMgrClass = null; // ConnectivityManager类
Field iConMgrField = null; // ConnectivityManager类中的字段
Object iConMgr = null; // IConnectivityManager类的引用
Class<?> iConMgrClass = null; // IConnectivityManager类
Method getMobileDataEnabledMethod = null; // setMobileDataEnabled方法
try {
// 取得ConnectivityManager类
conMgrClass = Class.forName(conMgr.getClass().getName());
// 取得ConnectivityManager类中的对象mService
iConMgrField = conMgrClass.getDeclaredField("mService");
// 设置mService可访问
iConMgrField.setAccessible(true);
// 取得mService的实例化类IConnectivityManager
iConMgr = iConMgrField.get(conMgr);
// 取得IConnectivityManager类
iConMgrClass = Class.forName(iConMgr.getClass().getName());
// 取得IConnectivityManager类中的getMobileDataEnabled(boolean)方法
getMobileDataEnabledMethod = iConMgrClass
.getDeclaredMethod("getMobileDataEnabled");
// 设置getMobileDataEnabled方法可访问
getMobileDataEnabledMethod.setAccessible(true);
// 调用getMobileDataEnabled方法
return (Boolean) getMobileDataEnabledMethod.invoke(iConMgr);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
/**
* 移动网络开关
*/
public void setMobileDataStatus(Context context, boolean enabled) {
ConnectivityManager conMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
Class<?> conMgrClass = null; // ConnectivityManager类
Field iConMgrField = null; // ConnectivityManager类中的字段
Object iConMgr = null; // IConnectivityManager类的引用
Class<?> iConMgrClass = null; // IConnectivityManager类
Method setMobileDataEnabledMethod = null; // setMobileDataEnabled方法
try {
// 取得ConnectivityManager类
conMgrClass = Class.forName(conMgr.getClass().getName());
// 取得ConnectivityManager类中的对象mService
iConMgrField = conMgrClass.getDeclaredField("mService");
// 设置mService可访问
iConMgrField.setAccessible(true);
// 取得mService的实例化类IConnectivityManager
iConMgr = iConMgrField.get(conMgr);
// 取得IConnectivityManager类
iConMgrClass = Class.forName(iConMgr.getClass().getName());
// 取得IConnectivityManager类中的setMobileDataEnabled(boolean)方法
setMobileDataEnabledMethod = iConMgrClass.getDeclaredMethod(
"setMobileDataEnabled", Boolean.TYPE);
// 设置setMobileDataEnabled方法可访问
setMobileDataEnabledMethod.setAccessible(true);
// 调用setMobileDataEnabled方法
setMobileDataEnabledMethod.invoke(iConMgr, enabled);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
public void toggleMobileData(Context context, boolean enabled) {
ConnectivityManager conMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
Class<?> conMgrClass = null;
Field iConMgrField = null;
Object iConMgr = null;
Class<?> iConMgrClass = null;
Method setMobileDataEnabledMethod = null;
String tag = "toggleMobileData()";
try {
conMgrClass = Class.forName(conMgr.getClass().getName());
iConMgrField = conMgrClass.getDeclaredField("mService");// mService
iConMgrField.setAccessible(true);
iConMgr = iConMgrField.get(conMgr);
iConMgrClass = Class.forName(iConMgr.getClass().getName());
setMobileDataEnabledMethod = iConMgrClass.getDeclaredMethod(
"setMobileDataEnabled", Boolean.TYPE);
setMobileDataEnabledMethod.setAccessible(true);
setMobileDataEnabledMethod.invoke(iConMgr, enabled);
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
e.printStackTrace();
} catch (NoSuchFieldException e) {
System.out.println(e.getMessage());
e.printStackTrace();
} catch (SecurityException e) {
System.out.println(e.getMessage());
e.printStackTrace();
} catch (NoSuchMethodException e) {
System.out.println(e.getMessage());
e.printStackTrace();
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
e.printStackTrace();
} catch (IllegalAccessException e) {
System.out.println(e.getMessage());
e.printStackTrace();
} catch (InvocationTargetException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
更多相关文章
- 浅谈Java中Collections.sort对List排序的两种方法
- Python list sort方法的具体使用
- python list.sort()根据多个关键字排序的方法实现
- Android(安卓)两种方式实现圆形头像
- Android获取、设置音频的音量大小
- FragmentTabHost QQ 选项卡
- Android常用控件技巧总结
- Android(安卓)自定义相机页面
- android SpannableStringBuilder设置自定义字体