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();

  }
 }

}

 



 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值