Android上關於cmwap/cmnet網路切換的疑惑?

來源:互聯網
上載者:User

           一、在網上看到一段代碼,是檢測當前行動電話通訊,並且自動切換到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"/>

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.