20117 月15
Android 开发:APN网络切换之CMNET
最近被Android系统的APN自动切换网络问题折腾死了,软件使用CMNET网络,而系统自带的一些软件必须使用CMWAP,或者手机厂家搞的一些后台服务或者流氓软件总是在切换网络。没办法,只好想个解决之道了。
我的解决方案是:
1、在程序启动时,注册 Receiver 监视网络状态,当网络发生变化判断不是CMNET时则切换网络;
2、为了保险起见,在每个HTTP链接请求前加上网络判断。
本软件主要实现了功能如下:
拍照、定位、表单文件上传、查询、短信拦截(用于通过短信指令获得手机当前位置)、拨打电话、定时自动上传定位数据、版本更新等。
如下粘贴APN网络判断网站代码:
1、NetworkChangeReceiver 网络状态监视
package com.wiz.receiver; import com.wiz.tools.NetCheck; import com.wiz.tools.StringUtils; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.util.Log; public class NetworkChangeReceiver extends BroadcastReceiver { NetCheck netCheck=new NetCheck(); public void onReceive(Context context, Intent intent) { Log.e("NetworkChangeReceiver", "onReceive"); ConnectivityManager conManager= (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) { NetworkInfo info = conManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); String apn = StringUtils.null2String(info.getExtraInfo()); if (!"cmnet".equals(apn.toLowerCase())) { netCheck.checkNetworkInfo(context); } } } }
2、Activity 中注册 NetworkChangeReceiver
NetworkChangeReceiver ncr = new NetworkChangeReceiver(); IntentFilter upIntentFilter = new IntentFilter( ConnectivityManager.CONNECTIVITY_ACTION); this.registerReceiver(ncr, upIntentFilter);// 网络状态监控
3、APN判断及网络切换
package com.wiz.tools; import android.content.ContentResolver; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.SQLException; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.Uri; import android.util.Log; public class NetCheck { public static final Uri APN_URI = Uri.parse("content://telephony/carriers"); public static final Uri CURRENT_APN_URI = Uri .parse("content://telephony/carriers/preferapn"); public static String getCurrentAPNFromSetting(ContentResolver resolver) { Cursor cursor = null; try { cursor = resolver.query(CURRENT_APN_URI, null, null, null, null); String curApnId = null; String apnName1=null; if (cursor != null && cursor.moveToFirst()) { curApnId = cursor.getString(cursor.getColumnIndex("_id")); apnName1 = cursor.getString(cursor.getColumnIndex("apn")); } cursor.close(); Log.e("NetCheck getCurrentAPNFromSetting","curApnId:"+curApnId+" apnName1:"+apnName1); //find apn name from apn list if (curApnId != null) { cursor = resolver.query(APN_URI, null, " _id = ?", new String[]{curApnId}, null); if (cursor != null && cursor.moveToFirst()) { String apnName = cursor.getString(cursor.getColumnIndex("apn")); return apnName; } } } catch (SQLException e) { Log.e("NetCheck getCurrentAPNFromSetting",e.getMessage()); } finally { if (cursor != null) { cursor.close(); } } return null; } public static int updateCurrentAPN(ContentResolver resolver, String newAPN) { Cursor cursor = null; try { //get new apn id from list cursor = resolver.query(APN_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(); Log.e("NetCheck updateCurrentAPN","apnId:"+apnId); //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) { Log.e("NetCheck updateCurrentAPN",e.getMessage()); } finally { if (cursor != null) { cursor.close(); } } //update success return 1; } public boolean checkNetworkInfo(Context c) { boolean ret=false; ConnectivityManager conManager= (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = conManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); boolean internet=conManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting(); String oldAPN = StringUtils.null2String(info.getExtraInfo()); String oldSQLAPN=StringUtils.null2String(getCurrentAPNFromSetting(c.getContentResolver())); Log.e("NetCheck checkNetworkInfo","oldAPN:"+oldAPN+" oldSQLAPN:"+oldSQLAPN); if (internet==false||!"cmnet".equals(oldAPN.toLowerCase())||!"cmnet".equals(oldSQLAPN.toLowerCase())) { if("cmwap".equals(oldAPN.toLowerCase())&&"cmnet".equals(oldSQLAPN.toLowerCase())){ updateCurrentAPN(c.getContentResolver(), "cmwap"); try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } } updateCurrentAPN(c.getContentResolver(), "cmnet"); try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } ret=true; } return ret; } }
4、HTTP请求前的判断
if (nc.checkNetworkInfo(LoginActivity.this)) { Thread.sleep(5000);// 设置cmnet网络 }
本文原创,转载请注明来源 wizzer.cn。
本文地址:https://wizzer.cn/archives/1836 , 转载请保留.