T
tc-maxx
Neues Mitglied
- 0
Hallo zusammen,
ich versuche mich an den APN-Einstellungen aber ich komme einfach nicht weiter.
Meine App soll einen neuen APN anlegen und diesen als default setzen.
Leider schmiert die Anwendung direkt nach dem Start ab:
( context.getContentResolver(... )
Wer findet den Fehler?
vg
ich versuche mich an den APN-Einstellungen aber ich komme einfach nicht weiter.
Meine App soll einen neuen APN anlegen und diesen als default setzen.
Leider schmiert die Anwendung direkt nach dem Start ab:
( context.getContentResolver(... )
Wer findet den Fehler?
vg
Code:
package de.apn;
import android.app.Activity;
import android.os.Bundle;
import android.content.ContentResolver;
import android.content.Context;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.net.Uri;
import android.util.Log;
public class APN_Test1Activity extends Activity {
private static final String TAG = "MyActivity";
public Context context;
//-[]----------------------------------------------------------------------------
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//--------------------------------------------------------------------------
EnumerateAPNs();
int id = InsertAPN("WEBBED","webbed.de");
SetDefaultAPN(id);
}
//-[ Information of all APNs]-----------------------------------------------------
public static final Uri APN_TABLE_URI = Uri.parse("content://telephony/carriers");
//-[ Information of the preferred APN]-------------------------------------------
public static final Uri PREFERRED_APN_URI = Uri.parse("content://telephony/carriers/preferapn");
//-[ Enumerate all APN data]------------------------------------------------------
private void EnumerateAPNs()
{
Cursor c = context.getContentResolver().query(APN_TABLE_URI, null, null, null, null);
if (c != null)
{
String s = "All APNs:\n";
Log.d(TAG, s);
try
{
s += printAllData(c); //Print the entire result set
}
catch(SQLException e)
{
Log.d(TAG, e.getMessage());
}
//Log.d(TAG, s + "\n\n");
c.close();
}
}
//-[Insert a new APN entry into the system APN table ]-----
//-[Return an id (_id) that is automatically generated for the new apn entry]-----
public int InsertAPN(String name, String apn_addr)
{
int id = -1;
ContentResolver resolver = context.getContentResolver();
ContentValues values = new ContentValues();
values.put("name", name);
values.put("apn", apn_addr);
values.put("mcc", "310");
values.put("mnc", "260");
values.put("numeric", "310260");
Cursor c = null;
try
{
Uri newRow = resolver.insert(APN_TABLE_URI, values);
if(newRow != null)
{
c = resolver.query(newRow, null, null, null, null);
Log.d(TAG, "Newly added APN:");
printAllData(c); //Print the entire result set
// 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;
}
//-[ Set an apn to be the default apn for web traffic ]--------------------------
//-[ Require an input of the apn id to be set ]--------------------------
public boolean SetDefaultAPN(int id)
{
boolean res = false;
ContentResolver resolver = context.getContentResolver();
ContentValues values = new ContentValues();
values.put("apn_id", id);
try
{
resolver.update(PREFERRED_APN_URI, values, null, null);
Cursor c = resolver.query(
PREFERRED_APN_URI,
new String[]{"name","apn"},
"_id="+id,
null,
null);
if(c != null)
{
res = true;
c.close();
}
}
catch (SQLException e)
{
Log.d(TAG, e.getMessage());
}
return res;
}
//-[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";
}
//-[Print all data records associated with Cursor c]------------------------------
//-[Return a string that contains all record data ]------------------------------
private String printAllData(Cursor c)
{
if(c == null) return null;
String s = "";
int record_cnt = c.getColumnCount();
Log.d(TAG, "Total # of records: " + record_cnt);
if(c.moveToFirst())
{
String[] columnNames = c.getColumnNames();
Log.d(TAG,getAllColumnNames(columnNames));
s += getAllColumnNames(columnNames);
do{
String row = "";
for(String columnIndex:columnNames)
{
int i = c.getColumnIndex(columnIndex);
row += c.getString(i)+":\t";
}
row += "\n";
Log.d(TAG, row);
s += row;
}while(c.moveToNext());
Log.d(TAG,"End Of Records");
}
return s;
}
}