概述
package com.bon.setapn;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;
/**
* 移动wap apn
* @author lei
*
*/
public class SetCmWap {
//添加APN共享数据类的Uri
final static String APN_URI = "content://telephony/carriers";
//设置默认APN选项的数据共享APN
final static String MAIN_APN = "content://telephony/carriers/preferapn";
final static String MY_APN = "移动wap";
private static int apnId = 0;
private static SharedPreferences sharedPreferences;
/**
* 添加新的APN选项并返回所添加的APNID
* @return id 所添加APN的ID
*/
private static int getAPNId(ContentResolver resolver) {
int id = -1;
ContentValues values = new ContentValues();
values.put("name", MY_APN);
values.put("apn", "cmWap");
values.put("proxy", "10.0.0.172");
values.put("port", "80");
values.put("numeric", "46002");
values.put("mcc", "460");
values.put("mnc", "02");
// ContentResolver resolver = getContentResolver();
Cursor c = null;
Uri newRow = resolver.insert(Uri.parse(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;
}
/**
* 设置默认的APN选项
* @param id 所要设置APN的系统数据库中所对应的ID
* 相关说明:通过数据共享类进行数据 共享Uri地址是:content://telephony/carriers/preferapn
*/
public static void setWapAPN(ContentResolver resolver, SharedPreferences _sharedPreferences) {
ContentValues values = new ContentValues();
sharedPreferences = _sharedPreferences;
apnId = sharedPreferences.getInt("apnId", 0);
if(apnId==0){
int id = getAPNId(resolver);
Log.i("MainActivity", "重新创建了一个");
values.put("apn_id", id);
Editor editor=sharedPreferences.edit(); //创建一个并保存id
editor.putInt("apnId", id);
editor.commit();
}else {
Log.i("MainActivity", "设置以前的");
values.put("apn_id", apnId);
}
resolver.update(Uri.parse(MAIN_APN), values, null, null);
}
/**
* 重新创建一个接入点 并设置
* @param resolver
* @param _sharedPreferences
*/
//防止用户自己删除以前创建的 删除之后 设置不了
public static void createWapApnAndSet(ContentResolver resolver, SharedPreferences _sharedPreferences) {
ContentValues values = new ContentValues();
sharedPreferences = _sharedPreferences;
apnId = sharedPreferences.getInt("apnId", 0);
Log.i("MainActivity", "保存的id"+apnId);
Log.i("MainActivity", "查到的id"+apnId);
Log.i("MainActivity", apnId+"");
int id = getAPNId(resolver);
Log.i("MainActivity", "重新创建了一个");
values.put("apn_id", id);
Editor editor=sharedPreferences.edit(); //创建并保存id
editor.putInt("apnId", id);
editor.commit();
resolver.update(Uri.parse(MAIN_APN), values, null, null);
}
}
最后
以上就是感性毛巾为你收集整理的apn java_android设置移动联通电信wap接入点的全部内容,希望文章能够帮你解决apn java_android设置移动联通电信wap接入点所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复