android 解决APN问题

来源:互联网 发布:咏春期权交易软件 编辑:程序博客网 时间:2024/05/17 21:54

android4.0之后,需要系统签名,并把apk放在system/app下面


[html] view plaincopy
  1. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >  
  2.    </uses-permission>  
  3.    <uses-permission android:name="android.permission.WRITE_APN_SETTINGS" >  
  4.    </uses-permission>  
  5.    <uses-permission android:name="android.permission.READ_PHONE_STATE" />  
  6.    <uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />  


[java] view plaincopy
  1. package com.lenovo.testapn;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.ContentResolver;  
  5. import android.content.ContentValues;  
  6. import android.content.Context;  
  7. import android.database.Cursor;  
  8. import android.net.Uri;  
  9. import android.os.Bundle;  
  10. import android.telephony.TelephonyManager;  
  11. import android.util.Log;  
  12. import android.view.Menu;  
  13.   
  14. public class MainActivity extends Activity {  
  15.   
  16.     private static final String TAG = "MainActivity";  
  17.   
  18.     public static final Uri APN_URI = Uri.parse("content://telephony/carriers");  
  19.   
  20.     // public static final Uri PREFERRED_APN_URI  
  21.     // =Uri.parse("PREFERRED_APN_URI  PREFERRED_APN_URI ");  
  22.     public static final Uri CURRENT_APN_URI = Uri  
  23.             .parse("content://telephony/carriers/preferapn");  
  24.   
  25.     @Override  
  26.     protected void onCreate(Bundle savedInstanceState) {  
  27.         super.onCreate(savedInstanceState);  
  28.         setContentView(R.layout.activity_main);  
  29.         Log.d(TAG, "onCreate");  
  30.         checkAPN();  
  31.         // SetAPN(addAPN());  
  32.     }  
  33.   
  34.     @Override  
  35.     public boolean onCreateOptionsMenu(Menu menu) {  
  36.         // Inflate the menu; this adds items to the action bar if it is present.  
  37.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  38.         return true;  
  39.     }  
  40.   
  41.     public void checkAPN() {  
  42.         // 检查当前连接的APN  
  43.         Cursor cr = getContentResolver().query(APN_URI, nullnullnullnull);  
  44.   
  45.         Log.d(TAG, "cr" + cr);  
  46.         while (cr != null && cr.moveToNext()) {  
  47.   
  48.             // if(cr.getString(cr.getColumnIndex("_id")))  
  49.   
  50.             // APN id  
  51.             String id = cr.getString(cr.getColumnIndex("_id"));  
  52.               
  53.             Log.d(TAG, "id" + id);  
  54.               
  55. //          String apn_id= cr.getString(cr.getColumnIndex("apn_id"));  
  56. //  
  57. //          Log.d(TAG, "apn_id" + apn_id);  
  58.             // APN name  
  59.             String apn = cr.getString(cr.getColumnIndex("apn"));  
  60.   
  61.             Log.d(TAG, apn);  
  62.             // Toast.makeText(getApplicationContext(),  
  63.             // "当前 id:" + id + " apn:" + apn, Toast.LENGTH_LONG).show();  
  64.         }  
  65.   
  66.     }  
  67.   
  68.     // 新增一个cmnet接入点  
  69.     public int addAPN() {  
  70.         int id = -1;  
  71.   
  72.         Log.d(TAG, "添加一个新的apn");  
  73.   
  74.         String NUMERIC = getSIMInfo();  
  75.   
  76.         Log.d(TAG, "NUMERIC" + NUMERIC);  
  77.   
  78.         if (NUMERIC == null) {  
  79.   
  80.             return -1;  
  81.         }  
  82.   
  83.         ContentResolver resolver = this.getContentResolver();  
  84.         ContentValues values = new ContentValues();  
  85.   
  86.         SIMCardInfo siminfo = new SIMCardInfo(MainActivity.this);  
  87.         // String user = siminfo.getNativePhoneNumber().substring(start);  
  88.   
  89.         values.put("name""专用APN"); // apn中文描述  
  90.         values.put("apn""myapn"); // apn名称  
  91.         values.put("type""default,supl");  
  92.         values.put("numeric", NUMERIC);  
  93.         values.put("mcc", NUMERIC.substring(03));  
  94.         values.put("mnc", NUMERIC.substring(3, NUMERIC.length()));  
  95.         values.put("proxy""");  
  96.         values.put("port""");  
  97.         values.put("mmsproxy""");  
  98.         values.put("mmsport""");  
  99.         values.put("user""");  
  100.         values.put("server""");  
  101.         values.put("password""");  
  102.         values.put("mmsc""");  
  103.   
  104.         Cursor c = null;  
  105.         Uri newRow = resolver.insert(APN_URI, values);  
  106.         if (newRow != null) {  
  107.             c = resolver.query(newRow, nullnullnullnull);  
  108.             int idIndex = c.getColumnIndex("_id");  
  109.             c.moveToFirst();  
  110.             id = c.getShort(idIndex);  
  111.         }  
  112.         if (c != null)  
  113.             c.close();  
  114.         return id;  
  115.     }  
  116.   
  117.     protected String getSIMInfo() {  
  118.         TelephonyManager iPhoneManager = (TelephonyManager) this  
  119.                 .getSystemService(Context.TELEPHONY_SERVICE);  
  120.         return iPhoneManager.getSimOperator();  
  121.     }  
  122.   
  123.     // 设置接入点  
  124.     public void SetAPN(int id) {  
  125.         ContentResolver resolver = this.getContentResolver();  
  126.         ContentValues values = new ContentValues();  
  127.         values.put("apn_id", id);  
  128.         resolver.update(CURRENT_APN_URI, values, nullnull);  
  129.   
  130.         // resolver.delete(url, where, selectionArgs)  
  131.     }  
  132.   
  133. }  
0 0
原创粉丝点击