一、在網上看到一段代碼,是檢測當前行動電話通訊,並且自動切換到Cmwap網路的demo,理論上是可行的,可以修改到cmwap的但是在實際運行中會報錯這樣的錯誤:
二、錯誤如下:
Caused by: java.lang.SecurityException: No permission to write APN settings: Neither user 10069 nor current process has android.permission.WRITE_APN_SETTINGS.
但是我已經加入了WRITE_APN_SETTINGS這個許可權,在網上搜了一下,有人說4.0以上版本,google禁掉了android.permission.WRITE_APN_SETTINGS,
然後有人給出了一個同樣問題的解決辦法有:
一種是應用有ROOT許可權,另外一種是設定APK的UID和system的一樣,關鍵是My Phone已經root過了,就剩下下面個問題了,現在還沒有找到解決辦法,如果有朋友有好的解決辦法,麻煩回複一下,多謝了。代碼在下面:
網友的問題:
網上搜了一下發現Android 2.3後不開放使用 MODIFY_PHONE_STATE
詳細參見: http://code.google.com/p/android/issues/detail?id=15031
http://stackoverflow.com/questions/4715250/how-to-grant-modify-phone-state-permission-for-apps-ran-on-gingerbread
有沒有人解決過這個問題呢?
網上搜尋有兩種解決方案,一種是應用有ROOT許可權,另外一種是設定APK的UID和system的一樣,可是都沒有具體的實現方法,而且這兩種方法也不是通用的。
有人實現過這個功能麼
//擷取當前APN屬性private boolean getCurrentAPN(){ PREFERRED_APN_URI = Uri.parse("content://telephony/carriers/preferapn"); cursor_current = this.getContentResolver().query(PREFERRED_APN_URI, null, null, null, null); if(cursor_current != null && cursor_current.moveToFirst()){ String proxy = cursor_current.getString(cursor_current.getColumnIndex("proxy")); String apn = cursor_current.getString(cursor_current.getColumnIndex("apn")); String port = cursor_current.getString(cursor_current.getColumnIndex("port")); String current = cursor_current.getString(cursor_current.getColumnIndex("current")); if(proxy == null || apn == null || port == null || current == null || proxy.equals("") || port.equals("")){ return false; } if((proxy.equals("10.0.0.172") || proxy.equals("010.000.000.172")) && port.equals("80") && apn.equals("cmwap") && current.equals("1")){ return true; } } return false; } //檢查是否存在cmwap網路 private boolean checkHasWapAPN(){ APN_TABLE_URI = Uri.parse("content://telephony/carriers"); cursor_need = this.getContentResolver().query(APN_TABLE_URI, null, null, null, null); while(cursor_need != null && cursor_need.moveToNext()){ String id = cursor_need.getString(cursor_need.getColumnIndex("_id")); String port = cursor_need.getString(cursor_need.getColumnIndex("port")); String proxy = cursor_need.getString(cursor_need.getColumnIndex("proxy")); String current = cursor_need.getString(cursor_need.getColumnIndex("current")); String mmsc = cursor_need.getString(cursor_need.getColumnIndex("mmsc")); if(proxy == null || port == null || current == null){ continue; } if((proxy.equals("10.0.0.172") || proxy.equals("010.000.000.172")) && port.equals("80") && current.equals("1") && mmsc == null){ APN_Id = id; return true; } } return false; } //設定為cmwap網路 public boolean setAPN(int id){ //如果wifi是開啟的,則關閉 wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE); if(wifi.isWifiEnabled()){ wifi.setWifiEnabled(false); } boolean res = false; ContentResolver resolver = this.getContentResolver(); ContentValues values = new ContentValues(); values.put("apn_id", id); try{ resolver.update(PREFERRED_APN_URI, values, null, null); Cursor c = resolver.query(PREFERRED_APN_URI, new String[]{"name", "apn"}, "_id=" + id, null, null); if(c != null){ res = true; c.close(); } }catch(SQLException e){ Log.e("lhl", e.getMessage()); } return res; } //添加cmwap網路 private int addCmwapAPN(){ ContentResolver cr = this.getContentResolver(); ContentValues cv = new ContentValues(); cv.put("name", "cmwap"); cv.put("apn", "cmwap"); cv.put("proxy", "010.000.000.172"); cv.put("port", "80"); cv.put("current", 1); tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); String imsi =tm.getSubscriberId(); if(imsi != null){ if(imsi.startsWith("46000")){ cv.put("numeric", "46000"); cv.put("mcc", "460"); cv.put("mnc", "00"); } else if(imsi.startsWith("46002")){ cv.put("numeric", "46002"); cv.put("mcc", "460"); cv.put("mnc", "02"); } } Cursor c = null; try{ Uri newRow = cr.insert(APN_TABLE_URI, cv); if(newRow != null){ c = cr.query(newRow, null, null, null, null); c.moveToFirst(); String id = c.getString(c.getColumnIndex("_id")); setAPN(Integer.parseInt(id)); return Integer.parseInt(id); } }catch(SQLException e){ Log.e("lhl", e.getMessage()); } finally{ if(c != null){ c.close(); } } return 0; }
//需要添加一些許可權: <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.WRITE_APN_SETTINGS"/> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.READ_PHONE_STATE"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>