android将网络连接强制修改为cmwap

 

import java.lang.reflect.Method;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;

public class UpdateCmwap {
 private TelephonyManager mTelephonyManager = null;
 public static final String LIST_APN_URI = "content://telephony/carriers";
 private static final Uri APN_LIST_URI = Uri.parse(LIST_APN_URI);
 public static final String PREFERRED_APN_URI = "content://telephony/carriers/preferapn";
 private static final Uri CURRENT_APN_URI = Uri.parse(PREFERRED_APN_URI);
 public Context context;
 public UpdateCmwap(Context context){
  this.context = context;
  boolean isSimExist = isSimExist();
  if (isSimExist) {
   boolean isMobile = isMobile();
   if (isMobile) {
//    closeWifi();
    int setMobile = setMobileDataEnabled(true);
    try {
     final int apiLevel = Build.VERSION.SDK_INT;
     if (apiLevel <= 10) {
      if (setMobile == 0) {
       closeWifi();
       updateCurrentAPN(context.getContentResolver(), "cmwap");
       Tool.info("cmwap", "您的卡是移动卡,联网方式将改为cmwap");
      }else {
       Tool.info("cmwap", "修改失败");
      }
     }else {
      Tool.info("cmwap", "您的api版本不支持");
      Tool.showToast(context, "请您打开数据连接");
//      // 设置APN选项
//         Intent intent = new Intent(Settings.ACTION_SETTINGS);
//         context.startActivity(intent);
     }
    } catch (Exception e) {
     // TODO: handle exception
     e.printStackTrace();
//     Intent intent = new Intent();
//     intent.setAction(Settings.ACTION_SETTINGS);
//        context.startActivity(intent);
//     WifiManager wifiManager;
//     wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
//     if (!wifiManager.isWifiEnabled()) {
//      wifiManager.setWifiEnabled(true);
//     }
    }
    
   }else {
    Tool.info("cmwap", "您的卡不是移动卡");
   }
  }else {
   Tool.showToast(context, "您的sim卡不可用,请检查您的sim卡是否插好");
  }
 }
 // 良好为卡可用
 private boolean isSimExist() {
  mTelephonyManager = (TelephonyManager) context
    .getSystemService(Context.TELEPHONY_SERVICE);
  int simState = mTelephonyManager.getSimState();
  if (simState == TelephonyManager.SIM_STATE_READY) {
   return true;
  } else {
   return false;
  }
  // switch (simState) {
  //
  // case TelephonyManager.SIM_STATE_ABSENT:
  // mString = "无卡";
  // // do something
  // break;
  //
  // case TelephonyManager.SIM_STATE_NETWORK_LOCKED:
  // mString = "需要NetworkPIN解锁";
  // // do something
  //
  // break;
  //
  // case TelephonyManager.SIM_STATE_PIN_REQUIRED:
  // mString = "需要PIN解锁";
  // // do something
  // break;
  //
  // case TelephonyManager.SIM_STATE_PUK_REQUIRED:
  // mString = "需要PUN解锁";
  // // do something
  // break;
  //
  // case TelephonyManager.SIM_STATE_READY:
  // mString = "良好";
  // // do something
  // break;
  //
  // case TelephonyManager.SIM_STATE_UNKNOWN:
  // mString = "未知状态";
  // // do something
  // break;
  // }
 }

 // true为移动
 private boolean isMobile() {
  boolean isMobile = false;
  TelephonyManager telManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
  // 获取SIM卡的IMSI码
  String imsi = telManager.getSubscriberId();
  // System.out.println("imsi==="+imsi);
  if (imsi != null && imsi.length() > 0) {
   if (imsi.startsWith("46000") || imsi.startsWith("46002")) {
    // 因为移动网络编号46000下的IMSI已经用完,所以虚拟了一个46002编号,134/159号段使用了此编号 //中国移动
    // System.out.println(imsi + "中国移动");
    isMobile = true;
   } else if (imsi.startsWith("46001")) {
    // 中国联通
    // System.out.println(imsi + "中国联通");
    isMobile = false;
   } else if (imsi.startsWith("46003")) {
    // 中国电信
    // System.out.println(imsi + "中国电信");
    isMobile = false;
   }
  }
  return isMobile;
 }
 /**
  * 关闭wifi
  */
 public void closeWifi(){
  WifiManager wifiManager;
  wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
  if (wifiManager.isWifiEnabled()) {
   wifiManager.setWifiEnabled(false);
  }
 }
 /**
  * 网络连接修改为数据连接
  * @param flag
  * @return
  */
 private int setMobileDataEnabled(boolean flag) {
  ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  Method setMobileDataEnabl;
  try {
   setMobileDataEnabl = cm.getClass().getDeclaredMethod(
     "setMobileDataEnabled", boolean.class);
   setMobileDataEnabl.invoke(cm, flag);
   return 0;
  } catch (Exception e) {
   e.printStackTrace();
   return -1;
  }
 }
 /**
  * 如果为移动,强制改为cmwap
  * @param resolver
  * @param newAPN
  * @return
  */
 public static int updateCurrentAPN(ContentResolver resolver, String newAPN) {
  Cursor cursor = null;
  try {
   // get new apn id from list
   cursor = resolver.query(APN_LIST_URI, null,
     " apn = ? and current = 1",
     new String[] { newAPN.toLowerCase() }, null);
   String apnId = null;
   if (cursor != null && cursor.moveToFirst()) {
    apnId = cursor.getString(cursor.getColumnIndex("_id"));
   }
   cursor.close();

   // set new apn id as chosen one
   if (apnId != null) {
    ContentValues values = new ContentValues();
    values.put("apn_id", apnId);
    resolver.update(CURRENT_APN_URI, values, null, null);
   } else {
    // apn id not found, return 0.
    return 0;
   }
  } catch (SQLException e) {

  } finally {
   if (cursor != null) {
    cursor.close();
   }
  }

  // update success
  return 1;
 }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值