zoukankan      html  css  js  c++  java
  • Android上关于cmwap/cmnet网络切换的疑惑?

               一、在网上看到一段代码,是检测当前手机网络,并且自动切换到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的一样,关键是我的手机已经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"/>
  • 相关阅读:
    HTTP协议
    jQuery中的事件模型
    AJAX请求 load方法的使用
    jQuery让页面动起来
    jQuery中的事件传播
    jQuery包装集和DOM对象
    CentOS 7 添加网卡后没有对应网卡配置文件解决方法
    openstack高可用集群搭建(集中式路由)(train版)
    5大富文本编辑器比较
    委托和事件:
  • 原文地址:https://www.cnblogs.com/yejiurui/p/2985155.html
Copyright © 2011-2022 走看看