android 設定APN

來源:互聯網
上載者:User

android4.0之後,需要系統簽名,並把apk放在system/app下面

 

 

[html]
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" > 
   </uses-permission> 
   <uses-permission android:name="android.permission.WRITE_APN_SETTINGS" > 
   </uses-permission> 
   <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
   <uses-permission android:name="android.permission.MODIFY_PHONE_STATE" /> 

 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
    </uses-permission>
    <uses-permission android:name="android.permission.WRITE_APN_SETTINGS" >
    </uses-permission>
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />

 

 

[java]
package com.lenovo.testapn; 
 
import android.app.Activity; 
import android.content.ContentResolver; 
import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.net.Uri; 
import android.os.Bundle; 
import android.telephony.TelephonyManager; 
import android.util.Log; 
import android.view.Menu; 
 
public class MainActivity extends Activity { 
 
    private static final String TAG = "MainActivity"; 
 
    public static final Uri APN_URI = Uri.parse("content://telephony/carriers"); 
 
    // public static final Uri PREFERRED_APN_URI  
    // =Uri.parse("PREFERRED_APN_URI  PREFERRED_APN_URI ");  
    public static final Uri CURRENT_APN_URI = Uri 
            .parse("content://telephony/carriers/preferapn"); 
 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
        Log.d(TAG, "onCreate"); 
        checkAPN(); 
        // SetAPN(addAPN());  
    } 
 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
        // Inflate the menu; this adds items to the action bar if it is present.  
        getMenuInflater().inflate(R.menu.activity_main, menu); 
        return true; 
    } 
 
    public void checkAPN() { 
        // 檢查當前串連的APN  
        Cursor cr = getContentResolver().query(APN_URI, null, null, null, null); 
 
        Log.d(TAG, "cr" + cr); 
        while (cr != null && cr.moveToNext()) { 
 
            // if(cr.getString(cr.getColumnIndex("_id")))  
 
            // APN id  
            String id = cr.getString(cr.getColumnIndex("_id")); 
             
            Log.d(TAG, "id" + id); 
             
//          String apn_id= cr.getString(cr.getColumnIndex("apn_id"));  
//  
//          Log.d(TAG, "apn_id" + apn_id);  
            // APN name  
            String apn = cr.getString(cr.getColumnIndex("apn")); 
 
            Log.d(TAG, apn); 
            // Toast.makeText(getApplicationContext(),  
            // "當前 id:" + id + " apn:" + apn, Toast.LENGTH_LONG).show();  
        } 
 
    } 
 
    // 新增一個cmnet存取點  
    public int addAPN() { 
        int id = -1; 
 
        Log.d(TAG, "添加一個新的apn"); 
 
        String NUMERIC = getSIMInfo(); 
 
        Log.d(TAG, "NUMERIC" + NUMERIC); 
 
        if (NUMERIC == null) { 
 
            return -1; 
        } 
 
        ContentResolver resolver = this.getContentResolver(); 
        ContentValues values = new ContentValues(); 
 
        SIMCardInfo siminfo = new SIMCardInfo(MainActivity.this); 
        // String user = siminfo.getNativePhoneNumber().substring(start);  
 
        values.put("name", "專用APN"); // apn中文描述  
        values.put("apn", "myapn"); // apn名稱  
        values.put("type", "default,supl"); 
        values.put("numeric", NUMERIC); 
        values.put("mcc", NUMERIC.substring(0, 3)); 
        values.put("mnc", NUMERIC.substring(3, NUMERIC.length())); 
        values.put("proxy", ""); 
        values.put("port", ""); 
        values.put("mmsproxy", ""); 
        values.put("mmsport", ""); 
        values.put("user", ""); 
        values.put("server", ""); 
        values.put("password", ""); 
        values.put("mmsc", ""); 
 
        Cursor c = null; 
        Uri newRow = resolver.insert(APN_URI, values); 
        if (newRow != null) { 
            c = resolver.query(newRow, null, null, null, null); 
            int idIndex = c.getColumnIndex("_id"); 
            c.moveToFirst(); 
            id = c.getShort(idIndex); 
        } 
        if (c != null) 
            c.close(); 
        return id; 
    } 
 
    protected String getSIMInfo() { 
        TelephonyManager iPhoneManager = (TelephonyManager) this 
                .getSystemService(Context.TELEPHONY_SERVICE); 
        return iPhoneManager.getSimOperator(); 
    } 
 
    // 設定存取點  
    public void SetAPN(int id) { 
        ContentResolver resolver = this.getContentResolver(); 
        ContentValues values = new ContentValues(); 
        values.put("apn_id", id); 
        resolver.update(CURRENT_APN_URI, values, null, null); 
 
        // resolver.delete(url, where, selectionArgs)  
    } 
 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.