InsertAPN()方法不写APN设置 - Android的2.3.6 [英] InsertAPN() Method Does Not Write APN Settings - Android 2.3.6
问题描述
我有这应该是我每次运行新的APN设置似乎并没有被写入到设备的应用程序时写入新的APN设置,但是设备的Android应用程序(APN设置在执行后完全空白)我相信的,这可能有事情做与InsertAPN(有问题)[如下]但是我无法确定问题的根源。
P.S。
任何调试提示/问题是很大的AP preciated。
源代码段:
/ *
*插入一个新的APN进入系统APN表需要APN名称,
* APN的地址。可以添加更多。返回一个ID(_id)是
*自动为新的APN条目中产生。
* /
公众诠释InsertAPN()抛出SecurityException异常{ INT ID = -1;
对于(i = 0; I<计数;我++){
ContentValues values2 =新ContentValues();
// values2 =值1;
values2 =的GetValues();
ContentResolver的解析器= getContentResolver();
光标C = NULL;
尝试{
乌里NEWROW = resolver.insert(APN_TABLE_URI,values2);
//的System.out.println(在insertAPN值+值1);
如果(NEWROW!= NULL){
C = resolver.query(NEWROW,NULL,NULL,NULL,NULL);
Log.d(TAG,新增APN:);
// TF设置已经插入
//获取APN ID
INT idindex = c.getColumnIndex(_ ID);
c.moveToFirst();
ID = c.getShort(idindex); Log.d(TAG,新ID:+ ID
+:插入新的APN成功了!);
}
}赶上(的SQLException E){
Log.d(TAG,e.getMessage());
}
如果(C!= NULL)
c.close();
}
返回ID;}
完整的源代码:
公共类ConfigFinalActivity扩展活动实现OnClickListener {
私有静态最后弦乐TAG =ConfigActivity;
TelephonyManager TM;
AlertDialog mErrorAlert = NULL;
民营通知mNotification = NULL;
私人按钮mXButton = NULL;
私人按钮mAssistUpdateButton = NULL;
私人按钮mAssistInstrButton = NULL;
私人按钮mReadAgainButton = NULL;
私人INT mInstructionNumber = 0;
公共静态的ArrayList<串GT; NameArr =新的ArrayList<串GT;();
公共静态的ArrayList<串GT; ValueArr =新的ArrayList<串GT;();
公共静态的ArrayList<串GT; nameArr =新的ArrayList<串GT;();
公共静态的ArrayList<串GT; ApnArr =新的ArrayList<串GT;();
公共静态的ArrayList<串GT; mmscArr =新的ArrayList<串GT;();
公共静态的ArrayList<串GT; mmsportArr =新的ArrayList<串GT;();
公共静态的ArrayList<串GT; mmsproxyArr =新的ArrayList<串GT;();
公共静态的ArrayList<串GT; portArr =新的ArrayList<串GT;();
公共静态的ArrayList<串GT; proxyArr =新的ArrayList<串GT;();
公共静态诠释计数;
公共静态INT TotalSteps = 8;
INT I,G = 0,结果为0; 公共静态ContentValues值=新ContentValues();
XmlParserHandlerFinal处理程序; 公共静态最终乌里APN_TABLE_URI =乌里
.parse(内容://电话/运营商);
公共静态最后弦乐BASE_URL =https://wapgate.example.com/REST/phoneSettings;
公共静态的InputStream流= NULL;
UpdateActivity更新;
公共静态字符串状态,QueryResult中=; / **当第一次创建活动调用。 * /
@燮pressLint(NewApi)
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
INT版本= android.os.Build.VERSION.SDK_INT;
TM =(TelephonyManager)getSystemService(TELEPHONY_SERVICE);
更新=新UpdateActivity();
处理器=新XmlParserHandlerFinal();
handler.setContext(this.getBaseContext());
getArrayLists();
/ *
*删除= DELETEFILE(settings.xml中),布尔;如果(删除){
* Log.v(settings.xml中,删除); }其他
* Log.v(settings.xml中,无法删除文件);
* /
如果(ApnArr.isEmpty()|| mmscArr.isEmpty()){ 再试一次();
}否则如果(版本< VERSION_ codeS.ICE_CREAM_SANDWICH){ //更新APN表
尝试{
结果= updateTable();
}赶上(IOException异常E1){
// TODO自动生成catch块
e1.printStackTrace();
} //设置这个原子呼叫更新
赶上(SAXException的E){
// TODO自动生成catch块
e.printStackTrace();
}赶上(的ParserConfigurationException E){
// TODO自动生成catch块
e.printStackTrace();
} 如果(结果!= -1){
状态=成功; }其他{
状态=失败;
} 如果(status.equals(成功)){
completeUpdate();
}否则如果(status.equals(失败)){
再试一次();
// showAlert(的getString(R.string.unchanged_dialog));
} }其他{// ICS和更高版本 //减少的步骤数6
TotalSteps = 6;
的setContentView(R.layout.assist_instructions);
字符串assistUpdate =的getString(R.string.instructions_1);
CharSequence中的StyledText = Html.fromHtml(assistUpdate);
TextView的assistText =(的TextView)findViewById(R.id.apn_app_text_cta2);
assistText.setText(的StyledText);
mAssistUpdateButton =(按钮)findViewById(R.id.assist_update_btn);
mAssistUpdateButton.setOnClickListener(本); }
} 私人无效getArrayLists(){
nameArr = update.getnameArr();
ApnArr = update.getApnArr();
mmscArr = update.getMMSCArr();
mmsproxyArr = update.getMmscProxyArr();
mmsportArr = update.getMmsPortArr();
proxyArr = update.getProxyArr();
portArr = update.getPortArr();
数= update.getCount();
QueryResult中= update.getResult();
} 公共无效的onClick(视图v){
如果(V == mAssistUpdateButton){ //为ICS和高达更新按钮被选中
//获取协助更新UI TextView的 TextView的电视=(的TextView)findViewById(R.id.apn_app_text_cta2);
字符串文本=;
CharSequence中的StyledText =文本;
开关(mInstructionNumber){ 情况下0:
//获得相应的指令字符串资源
//第二指令集
文字=的String.format(的getString(R.string.apn_app_text_instr)
TotalSteps);
的StyledText = Html.fromHtml(文本);
//更新与正确的指令集的TextView的
tv.setText(的StyledText);
//增加指令数,所以正确的指令
//字符串资源可以进行下一次检索更新
//按钮为pressed
mInstructionNumber ++;
打破;
情况1:
文字=的getString(R.string.apn_app_text_instr2);
的StyledText = Html.fromHtml(文本);
tv.setText(的StyledText);
//增加指令数,所以正确的指令
//字符串资源可以进行下一次检索更新
//按钮为pressed
mInstructionNumber ++;
打破;
案例2:
//决胜盘说明,更改到相应的布局 的setContentView(R.layout.assist_instructions);
字符串assistUpdateInstr =的String.format(
的getString(R.string.apn_app_text_instr3),TotalSteps);
的StyledText = Html.fromHtml(assistUpdateInstr);
TextView的assistInstrText =(的TextView)findViewById(R.id.updated_text);
assistInstrText.setText(的StyledText);
mAssistInstrButton =(按钮)findViewById(R.id.assist_instr_btn); mAssistInstrButton.setOnClickListener(本); }
}否则如果(V == mAssistInstrButton){
//让我们做到这一点按钮,在最后的指示屏幕和ICS
//中选择了
值=的GetValues();
startActivity(新意图(Settings.ACTION_APN_SETTINGS));
尝试{
showNotification();
}赶上(SAXException的E){
// TODO自动生成catch块
e.printStackTrace();
}赶上(的ParserConfigurationException E){
// TODO自动生成catch块
e.printStackTrace();
}
完();
}否则如果(V == mAssistInstrButton){
startActivity(新意图(Settings.ACTION_APN_SETTINGS));
尝试{
showNotification();
}赶上(E1的SAXException){
// TODO自动生成catch块
e1.printStackTrace();
}赶上(E1的ParserConfigurationException){
// TODO自动生成catch块
e1.printStackTrace();
}
尝试{
showNotification();
}赶上(SAXException的E){
// TODO自动生成catch块
e.printStackTrace();
}赶上(的ParserConfigurationException E){
// TODO自动生成catch块
e.printStackTrace();
}
完();
}
} / *
*插入一个新的APN进入系统APN表需要APN名称,
* APN的地址。可以添加更多。返回一个ID(_id)是
*自动为新的APN条目中产生。
* /
公众诠释InsertAPN()抛出SecurityException异常{ INT ID = -1;
对于(i = 0; I<计数;我++){
ContentValues values2 =新ContentValues();
// values2 =值1;
values2 =的GetValues();
ContentResolver的解析器= getContentResolver();
光标C = NULL;
尝试{
乌里NEWROW = resolver.insert(APN_TABLE_URI,values2);
//的System.out.println(在insertAPN值+值1);
如果(NEWROW!= NULL){
C = resolver.query(NEWROW,NULL,NULL,NULL,NULL);
Log.d(TAG,新增APN:);
// TF设置已经插入
//获取APN ID
INT idindex = c.getColumnIndex(_ ID);
c.moveToFirst();
ID = c.getShort(idindex); Log.d(TAG,新ID:+ ID
+:插入新的APN成功了!);
}
}赶上(的SQLException E){
Log.d(TAG,e.getMessage());
}
如果(C!= NULL)
c.close();
}
返回ID; } 公共ContentValues的GetValues(){
ContentValues值=新ContentValues(); values.put(名,nameArr.get(I));
values.put(APN,ApnArr.get(I));
values.put(MMSC,mmscArr.get(ⅰ));
//values.put(\"mmsproxy,mmsproxyArr.get(ⅰ));
//values.put(\"mmsport,mmsportArr.get(ⅰ));
// values.put(代理,proxyArr.get(I));
values.put(港,portArr.get(I));
values.put(MCC(的getString(R.string.mcc)));
如果((tm.getSimOperator())。等于(的getString(R.string.numeric_tmo))){
values.put(数字,的getString(R.string.numeric_tmo));
values.put(MNC,(的getString(R.string.mnc_tmo)));
}否则如果((tm.getSimOperator())
.equals(的getString(R.string.numeric_att))){
values.put(数字,的getString(R.string.numeric_att));
values.put(MNC,(的getString(R.string.mnc_att)));
}
返回值;
}
/ *
*删除APN数据,其中显示字段的值整个表
*如果删除了现场和为null
* /
私人无效DeleteAPNs(String域,的String []值)
抛出SecurityException异常{
INT C = 0;
C = getContentResolver()删除(APN_TABLE_URI,NULL,NULL);
如果(C!= 0){
字符串s =删除的APN:\\ N的;
Log.d(TAG,S); } } / *
*返回保存在字符串数组中的所有列名
* /
私人字符串getAllColumnNames(字符串[] COLUMNNAMES){
字符串s =列名:\\ n;
对(串T:COLUMNNAMES){
S + = T +:\\ t的;
}
返回S +\\ n;
} / *
*复制与第1记录光标C相关联的所有数据。返回一个
*包含所有记录数据ContentValues。
* /
私人ContentValues copyRecordFields(光标C){
如果(C == NULL)
返回null;
INT row_cnt = c.getCount();
Log.d(TAG,总记录#:+ row_cnt);
ContentValues值=新ContentValues(); //
如果(c.moveToFirst()){
的String [] = COLUMNNAMES c.getColumnNames();
Log.d(TAG,getAllColumnNames(COLUMNNAMES));
字串行=;
对于(字符串参数:columnIndex:COLUMNNAMES){
INT I = c.getColumnIndex(参数:columnIndex);
行+ = c.getString(ⅰ)+:\\ t的;
//如果(我大于0)//避免复制ID字段
//要根据记录的插入自动生成的ID
values.put(参数:columnIndex,c.getString(I));
}
排+ =\\ n;
Log.d(TAG,行);
Log.d(TAG,结束记录);
}
返回值;
} // showAlert显示包含在消息作为警报文本
公共无效showAlert(字符串消息){
AlertDialog.Builder建设者=新AlertDialog.Builder(本);
builder.setMessage(消息).setPositiveButton(OK,
新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话,诠释的id){
ConfigFinalActivity.this.finish();
}
});
mErrorAlert = builder.create();
mErrorAlert.show();
} // showErrorAlert显示与布局和标题的警报
私人无效showErrorAlert(INT layoutRes,字符串名称){
AlertDialog.Builder建设者=新AlertDialog.Builder(本);
//获取吹气布局
LayoutInflater吹气= ConfigFinalActivity.this.getLayoutInflater(); //充气并为对话设置布局
作为父视图//传递空值,因为它在该对话框的布局会
builder.setTitle(职称)
.setView(inflater.inflate(layoutRes,NULL))
.setPositiveButton(的getString(R.string.assisted_button)
新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话,诠释的id){
startActivity(新意图(
Settings.ACTION_APN_SETTINGS));
尝试{
showNotification();
}赶上(SAXException的E){
// TODO自动生成catch块
e.printStackTrace();
}赶上(的ParserConfigurationException E){
// TODO自动生成catch块
e.printStackTrace();
}
}
});
mErrorAlert = builder.create();
mErrorAlert.show();
} // showNotification开始发送通知栏的过程
//帮助用户在更新的ICS和后来的数据设置
//的Android版本
@燮pressWarnings(德precation)
@TargetApi(Build.VERSION_ codeS.ICE_CREAM_SANDWICH)
公共无效showNotification()抛出的SAXException,{的ParserConfigurationException 字符串字段=的getString(R.string.config_name_label); 字符串值= Values.get(名称)的toString()。
INT MID = 1;
字符串标题= + UpdateActivity.TotalSteps +(更新,1
+现场+:)
NotificationCompat.Builder mBuilder =新NotificationCompat.Builder(本)
.setSmallIcon(R.drawable.notifications_icon)
.setContentTitle(职称).setContentText(值);
意图resultIntent =新意图(这一点,
NotificationActivityForMultiProf.class);
resultIntent.putExtra(场,值);
的PendingIntent resultPendingIntent = PendingIntent.getActivity(
getApplicationContext(),0,resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
// MID,您可以稍后更新通知。
mNotification = mBuilder.getNotification();
mNotification.flags | = Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(MID,mNotification);
完();
} @覆盖
保护无效的onSaveInstanceState(捆绑outState){
// TODO自动生成方法存根
super.onSaveInstanceState(outState);
如果(mNotification!= NULL){
outState.putString(NOTIFICATIONB,mNotification.toString());
}
} @覆盖
保护无效onRestart(){
super.onRestart();
如果(mErrorAlert!= NULL)
mErrorAlert.dismiss();
} 私人诠释updateTable()抛出IOException异常,SAXException中,
{的ParserConfigurationException
INT insertResult = -1;
//返回值,如果表是不正确地更新 尝试{ ContentValues值=新ContentValues();
//查询载波表当前的数据设定
光标C = getContentResolver()查询(APN_TABLE_URI,空,
当前=?,新的String [] {1},NULL);
值= copyRecordFields(C);
//设置复制到值 //替换T型钼/ ATT数据设置,如果没有SIM卡或
// NET10 / T型钼/ ATT SIM卡
// present
如果(tm.getSimState()== TelephonyManager.SIM_STATE_ABSENT
|| (tm.getSimOperator())
.equals(的getString(R.string.numeric_tmo))){ //添加新的APN之前删除所有的APN
DeleteAPNs(数字=?,
新的String [] {的getString(R.string.numeric_tmo)});
//将数据设置成载波表 insertResult = InsertAPN(); }否则如果(tm.getSimState()== TelephonyManager.SIM_STATE_ABSENT
|| (tm.getSimOperator())
.equals(的getString(R.string.numeric_att))){
//添加新的APN之前删除所有的APN
DeleteAPNs(数字=?,
新的String [] {的getString(R.string.numeric_att)});
//将数据设置成载波表 insertResult = InsertAPN(); }其他
//非NET10 /非T型钼SIM /非ATT SIM卡
showAlert(的getString(R.string.insert_sm_dialog));
}赶上(SecurityException异常五){
showErrorAlert(R.layout.assisted_setting,
的getString(R.string.assited_title));
Log.d(TAG,e.getMessage());
}
返回insertResult;
} 私人无效completeUpdate(){
//显示pre-ICS自动设置,更新后的最终布局
的setContentView(R.layout.completion);
TextView的mCompleted =(的TextView)findViewById(R.id.done_text);
字符串mDoneText =的String.format(的getString(R.string.done_text));
CharSequence中的StyledText = Html.fromHtml(mDoneText); mCompleted.setText(的StyledText);
mXButton =(按钮)findViewById(R.id.x_button);
mXButton.setOnClickListener(新OnClickListener(){ @覆盖
公共无效的onClick(视图v){
完();
}
});
} 公共无效TRYAGAIN(){
//的pre-ICS自动设置失败后显示的最终布局
//更新
的setContentView(R.layout.tryagain);
串tryAgainText =;
CharSequence的styledTryAgainText; tryAgainText =的String.format(的getString(R.string.tryagain_text1)
TotalSteps);
styledTryAgainText = Html.fromHtml(tryAgainText);
TextView的tryAgain1 =(的TextView)findViewById(R.id.tryagain_text1);
tryAgain1.setText(styledTryAgainText); tryAgainText =的String.format(的getString(R.string.tryagain_text2)
TotalSteps);
styledTryAgainText = Html.fromHtml(tryAgainText);
TextView的tryAgain2 =(的TextView)findViewById(R.id.tryagain_text2);
tryAgain2.setText(styledTryAgainText); tryAgainText =的String.format(的getString(R.string.tryagain_text3)
TotalSteps);
styledTryAgainText = Html.fromHtml(tryAgainText);
TextView的tryAgain3 =(的TextView)findViewById(R.id.tryagain_text3);
tryAgain3.setText(styledTryAgainText); } //这个函数返回一个指针表持有
//将APN配置(载波表)
公共光标getConfigTableCursor(){
返回getContentResolver()
.query(APN_TABLE_URI,NULL,NULL,NULL,NULL);
}}
您不能再编程上的Android 4.0及以上更改APN设置。所以,如果你在Android 4.0或更高版本的调试,这是你的问题。
I have an android application which is supposed to write new APN settings to a device however every time I run the application the new APN settings do not appear to be written to the device (the APN settings are totally blank after execution) I belive this may have something to do with a problem with InsertAPN() [shown below] however I'm unable to determine the root cause of the issue.
P.S.
Any debugging tips/questions are greatly appreciated.
SOURCE SNIPPET:
/*
* Insert a new APN entry into the system APN table Require an apn name, and
* the apn address. More can be added. Return an id (_id) that is
* automatically generated for the new apn entry.
*/
public int InsertAPN() throws SecurityException {
int id = -1;
for (i = 0; i < count; i++) {
ContentValues values2 = new ContentValues();
// values2 = values1;
values2 = getValues();
ContentResolver resolver = getContentResolver();
Cursor c = null;
try {
Uri newRow = resolver.insert(APN_TABLE_URI, values2);
// System.out.println("values in insertAPN" + values1);
if (newRow != null) {
c = resolver.query(newRow, null, null, null, null);
Log.d(TAG, "Newly added APN:");
// TF Settings have been inserted
// Obtain the apn id
int idindex = c.getColumnIndex("_id");
c.moveToFirst();
id = c.getShort(idindex);
Log.d(TAG, "New ID: " + id
+ ": Inserting new APN succeeded!");
}
} catch (SQLException e) {
Log.d(TAG, e.getMessage());
}
if (c != null)
c.close();
}
return id;
}
FULL SOURCE:
public class ConfigFinalActivity extends Activity implements OnClickListener {
private static final String TAG = "ConfigActivity";
TelephonyManager tm;
AlertDialog mErrorAlert = null;
private Notification mNotification = null;
private Button mXButton = null;
private Button mAssistUpdateButton = null;
private Button mAssistInstrButton = null;
private Button mReadAgainButton = null;
private int mInstructionNumber = 0;
public static ArrayList<String> NameArr = new ArrayList<String>();
public static ArrayList<String> ValueArr = new ArrayList<String>();
public static ArrayList<String> nameArr = new ArrayList<String>();
public static ArrayList<String> ApnArr = new ArrayList<String>();
public static ArrayList<String> mmscArr = new ArrayList<String>();
public static ArrayList<String> mmsportArr = new ArrayList<String>();
public static ArrayList<String> mmsproxyArr = new ArrayList<String>();
public static ArrayList<String> portArr = new ArrayList<String>();
public static ArrayList<String> proxyArr = new ArrayList<String>();
public static int count;
public static int TotalSteps = 8;
int i, g = 0, result = 0;
public static ContentValues Values = new ContentValues();
XmlParserHandlerFinal handler;
public static final Uri APN_TABLE_URI = Uri
.parse("content://telephony/carriers");
public static final String Base_URL = "https://wapgate.example.com/REST/phoneSettings";
public static InputStream stream = null;
UpdateActivity update;
public static String status, queryResult = "";
/** Called when the activity is first created. */
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int version = android.os.Build.VERSION.SDK_INT;
tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
update = new UpdateActivity();
handler = new XmlParserHandlerFinal();
handler.setContext(this.getBaseContext());
getArrayLists();
/*
* boolean deleted = deleteFile("settings.xml");if(deleted){
* Log.v("settings.xml","deleted"); }else
* Log.v("settings.xml","failed to delete the file");
*/
if (ApnArr.isEmpty() || mmscArr.isEmpty()) {
tryagain();
} else if (version < VERSION_CODES.ICE_CREAM_SANDWICH) {
// Update APN table
try {
result = updateTable();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}// Settings updated with this atomic call
catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (result != -1) {
status = "success";
} else {
status = "failure";
}
if (status.equals("success")) {
completeUpdate();
} else if (status.equals("failure")) {
tryagain();
// showAlert(getString(R.string.unchanged_dialog));
}
} else {// ICS and later versions
// Reduce number of steps to 6
TotalSteps = 6;
setContentView(R.layout.assist_instructions);
String assistUpdate = getString(R.string.instructions_1);
CharSequence styledText = Html.fromHtml(assistUpdate);
TextView assistText = (TextView) findViewById(R.id.apn_app_text_cta2);
assistText.setText(styledText);
mAssistUpdateButton = (Button) findViewById(R.id.assist_update_btn);
mAssistUpdateButton.setOnClickListener(this);
}
}
private void getArrayLists() {
nameArr = update.getnameArr();
ApnArr = update.getApnArr();
mmscArr = update.getMMSCArr();
mmsproxyArr = update.getMmscProxyArr();
mmsportArr = update.getMmsPortArr();
proxyArr = update.getProxyArr();
portArr = update.getPortArr();
count = update.getCount();
queryResult = update.getResult();
}
public void onClick(View v) {
if (v == mAssistUpdateButton) {
// Update button for ICS and up is selected
// Get the TextView in the Assist Update UI
TextView tv = (TextView) findViewById(R.id.apn_app_text_cta2);
String text = "";
CharSequence styledText = text;
switch (mInstructionNumber) {
case 0:
// Retrieve the instruction string resource corresponding the
// 2nd set of instructions
text = String.format(getString(R.string.apn_app_text_instr),
TotalSteps);
styledText = Html.fromHtml(text);
// Update the TextView with the correct set of instructions
tv.setText(styledText);
// Increment instruction number so the correct instructions
// string resource can be retrieve the next time the update
// button is pressed
mInstructionNumber++;
break;
case 1:
text = getString(R.string.apn_app_text_instr2);
styledText = Html.fromHtml(text);
tv.setText(styledText);
// Increment instruction number so the correct instructions
// string resource can be retrieve the next time the update
// button is pressed
mInstructionNumber++;
break;
case 2:
// Final set of instructions-Change to the corresponding layout
setContentView(R.layout.assist_instructions);
String assistUpdateInstr = String.format(
getString(R.string.apn_app_text_instr3), TotalSteps);
styledText = Html.fromHtml(assistUpdateInstr);
TextView assistInstrText = (TextView) findViewById(R.id.updated_text);
assistInstrText.setText(styledText);
mAssistInstrButton = (Button) findViewById(R.id.assist_instr_btn);
mAssistInstrButton.setOnClickListener(this);
}
} else if (v == mAssistInstrButton) {
// "LET'S DO THIS" Button in final instructions screen for ICS and
// up is selected
Values = getValues();
startActivity(new Intent(Settings.ACTION_APN_SETTINGS));
try {
showNotification();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finish();
} else if (v == mAssistInstrButton) {
startActivity(new Intent(Settings.ACTION_APN_SETTINGS));
try {
showNotification();
} catch (SAXException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ParserConfigurationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
showNotification();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finish();
}
}
/*
* Insert a new APN entry into the system APN table Require an apn name, and
* the apn address. More can be added. Return an id (_id) that is
* automatically generated for the new apn entry.
*/
public int InsertAPN() throws SecurityException {
int id = -1;
for (i = 0; i < count; i++) {
ContentValues values2 = new ContentValues();
// values2 = values1;
values2 = getValues();
ContentResolver resolver = getContentResolver();
Cursor c = null;
try {
Uri newRow = resolver.insert(APN_TABLE_URI, values2);
// System.out.println("values in insertAPN" + values1);
if (newRow != null) {
c = resolver.query(newRow, null, null, null, null);
Log.d(TAG, "Newly added APN:");
// TF Settings have been inserted
// Obtain the apn id
int idindex = c.getColumnIndex("_id");
c.moveToFirst();
id = c.getShort(idindex);
Log.d(TAG, "New ID: " + id
+ ": Inserting new APN succeeded!");
}
} catch (SQLException e) {
Log.d(TAG, e.getMessage());
}
if (c != null)
c.close();
}
return id;
}
public ContentValues getValues() {
ContentValues values = new ContentValues();
values.put("name", nameArr.get(i));
values.put("apn", ApnArr.get(i));
values.put("mmsc", mmscArr.get(i));
//values.put("mmsproxy", mmsproxyArr.get(i));
//values.put("mmsport", mmsportArr.get(i));
// values.put("proxy", proxyArr.get(i));
values.put("port", portArr.get(i));
values.put("mcc", (getString(R.string.mcc)));
if ((tm.getSimOperator()).equals(getString(R.string.numeric_tmo))) {
values.put("numeric", getString(R.string.numeric_tmo));
values.put("mnc", (getString(R.string.mnc_tmo)));
} else if ((tm.getSimOperator())
.equals(getString(R.string.numeric_att))) {
values.put("numeric", getString(R.string.numeric_att));
values.put("mnc", (getString(R.string.mnc_att)));
}
return values;
}
/*
* Delete APN data where the indicated field has the values Entire table is
* deleted if both field and value are null
*/
private void DeleteAPNs(String field, String[] values)
throws SecurityException {
int c = 0;
c = getContentResolver().delete(APN_TABLE_URI, null, null);
if (c != 0) {
String s = "APNs Deleted:\n";
Log.d(TAG, s);
}
}
/*
* Return all column names stored in the string array
*/
private String getAllColumnNames(String[] columnNames) {
String s = "Column Names:\n";
for (String t : columnNames) {
s += t + ":\t";
}
return s + "\n";
}
/*
* Copy all data associated with the 1st record Cursor c. Return a
* ContentValues that contains all record data.
*/
private ContentValues copyRecordFields(Cursor c) {
if (c == null)
return null;
int row_cnt = c.getCount();
Log.d(TAG, "Total # of records: " + row_cnt);
ContentValues values = new ContentValues();//
if (c.moveToFirst()) {
String[] columnNames = c.getColumnNames();
Log.d(TAG, getAllColumnNames(columnNames));
String row = "";
for (String columnIndex : columnNames) {
int i = c.getColumnIndex(columnIndex);
row += c.getString(i) + ":\t";
// if (i>0)//Avoid copying the id field
// id to be auto-generated upon record insertion
values.put(columnIndex, c.getString(i));
}
row += "\n";
Log.d(TAG, row);
Log.d(TAG, "End Of Records");
}
return values;
}
// showAlert displays the text contained in message as an alert
public void showAlert(String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(message).setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
ConfigFinalActivity.this.finish();
}
});
mErrorAlert = builder.create();
mErrorAlert.show();
}
// showErrorAlert displays an alert with layout and a title
private void showErrorAlert(int layoutRes, String title) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// Get the layout inflater
LayoutInflater inflater = ConfigFinalActivity.this.getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setTitle(title)
.setView(inflater.inflate(layoutRes, null))
.setPositiveButton(getString(R.string.assisted_button),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
startActivity(new Intent(
Settings.ACTION_APN_SETTINGS));
try {
showNotification();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
mErrorAlert = builder.create();
mErrorAlert.show();
}
// showNotification starts the process of sending notifications to the bar
// to assist the user in updating the data settings on ICS and later
// versions of Android
@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public void showNotification() throws SAXException, ParserConfigurationException {
String field = getString(R.string.config_name_label);
String value = Values.get("name").toString();
int mId = 1;
String title = "1 of " + UpdateActivity.TotalSteps + " (Update "
+ field + ":)";
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notifications_icon)
.setContentTitle(title).setContentText(value);
Intent resultIntent = new Intent(this,
NotificationActivityForMultiProf.class);
resultIntent.putExtra(field, value);
PendingIntent resultPendingIntent = PendingIntent.getActivity(
getApplicationContext(), 0, resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotification = mBuilder.getNotification();
mNotification.flags |= Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(mId, mNotification);
finish();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
if (mNotification != null) {
outState.putString("NOTIFICATIONB", mNotification.toString());
}
}
@Override
protected void onRestart() {
super.onRestart();
if (mErrorAlert != null)
mErrorAlert.dismiss();
}
private int updateTable() throws IOException, SAXException,
ParserConfigurationException {
int insertResult = -1;
// returned value if table is not properly updated
try {
ContentValues values = new ContentValues();
// Query the carrier table for the current data settings
Cursor c = getContentResolver().query(APN_TABLE_URI, null,
"current=?", new String[] { "1" }, null);
values = copyRecordFields(c);
// Copy the settings into values
// Replace T-Mo/ATT Data settings if there is no SIM or
// NET10/T-Mo/ATT SIM is
// present
if (tm.getSimState() == TelephonyManager.SIM_STATE_ABSENT
|| (tm.getSimOperator())
.equals(getString(R.string.numeric_tmo))) {
// delete all APNs before adding new APNs
DeleteAPNs("numeric=?",
new String[] { getString(R.string.numeric_tmo) });
// Insert Data Settings into Carrier table
insertResult = InsertAPN();
} else if (tm.getSimState() == TelephonyManager.SIM_STATE_ABSENT
|| (tm.getSimOperator())
.equals(getString(R.string.numeric_att))) {
// Delete all APNs before adding new APNs
DeleteAPNs("numeric=?",
new String[] { getString(R.string.numeric_att) });
// Insert Data Settings into Carrier table
insertResult = InsertAPN();
} else
// non NET10/ non T-Mo SIM/non ATT SIM
showAlert(getString(R.string.insert_sm_dialog));
} catch (SecurityException e) {
showErrorAlert(R.layout.assisted_setting,
getString(R.string.assited_title));
Log.d(TAG, e.getMessage());
}
return insertResult;
}
private void completeUpdate() {
// Displaying final layout after pre-ICS automatic settings update
setContentView(R.layout.completion);
TextView mCompleted = (TextView) findViewById(R.id.done_text);
String mDoneText = String.format(getString(R.string.done_text));
CharSequence styledText = Html.fromHtml(mDoneText);
mCompleted.setText(styledText);
mXButton = (Button) findViewById(R.id.x_button);
mXButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
public void tryagain() {
// Displaying final layout after failure of pre-ICS automatic settings
// update
setContentView(R.layout.tryagain);
String tryAgainText = "";
CharSequence styledTryAgainText;
tryAgainText = String.format(getString(R.string.tryagain_text1),
TotalSteps);
styledTryAgainText = Html.fromHtml(tryAgainText);
TextView tryAgain1 = (TextView) findViewById(R.id.tryagain_text1);
tryAgain1.setText(styledTryAgainText);
tryAgainText = String.format(getString(R.string.tryagain_text2),
TotalSteps);
styledTryAgainText = Html.fromHtml(tryAgainText);
TextView tryAgain2 = (TextView) findViewById(R.id.tryagain_text2);
tryAgain2.setText(styledTryAgainText);
tryAgainText = String.format(getString(R.string.tryagain_text3),
TotalSteps);
styledTryAgainText = Html.fromHtml(tryAgainText);
TextView tryAgain3 = (TextView) findViewById(R.id.tryagain_text3);
tryAgain3.setText(styledTryAgainText);
}
// This function return a cursor to the table holding the
// the APN configurations (Carrier table)
public Cursor getConfigTableCursor() {
return getContentResolver()
.query(APN_TABLE_URI, null, null, null, null);
}
}
You can no longer programatically change APN settings on Android 4.0 and up. So if you're debugging on Android 4.0 or higher, that is where your issue is.
这篇关于InsertAPN()方法不写APN设置 - Android的2.3.6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!