android 设置APN

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


 
    
    
    
    
    


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)
	}

}


你可能感兴趣的:(Android)