APN erstellen und setzen

T

tc-maxx

Neues Mitglied
Threadstarter
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? :rolleyes2:

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;
    }
 }
 
DieGoldeneMitte

DieGoldeneMitte

Experte
Fehler nummer 1: Du hast keinen LogCat gepostet. :D
 
T

tc-maxx

Neues Mitglied
Threadstarter
up's :)

Code:
Tag:                   Text:
D/AndroidRuntime(208): Shutting down VM
W/dalvikvm(208)      : threadid=3: thread exiting with uncaught exception (group=0x4001b188)
E/AndroidRuntime(208): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime(208): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.APN/de.APN.APNActivity}: java.lang.NullPointerException
E/AndroidRuntime(208):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
E/AndroidRuntime(208):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
E/AndroidRuntime(208):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
E/AndroidRuntime(208):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
E/AndroidRuntime(208):  at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(208):  at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(208):  at android.app.ActivityThread.main(ActivityThread.java:4363)
E/AndroidRuntime(208):  at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(208):  at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(208):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
E/AndroidRuntime(208):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
E/AndroidRuntime(208):  at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(208): Caused by: java.lang.NullPointerException
E/AndroidRuntime(208):  at de.APN.APNActivity.EnumerateAPNs(APNActivity.java:39)
E/AndroidRuntime(208):  at de.APN.APNActivity.onCreate(APNActivity.java:25)
E/AndroidRuntime(208):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime(208):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
E/AndroidRuntime(208):  ... 11 more
I/dalvikvm(208)      : threadid=7: reacting to signal 3
E/dalvikvm(208)      : Unable to open stack trace file '/data/anr/traces.txt': Permission denied
 
S

swordi

Gewerbliches Mitglied
der fehler ist in der apnactivity

du hast den code der APN_Test1Activity gepostet

und auch wenn das der richtige code ist

du schreibst oben
Context context;

dann context.getContentResolver

ist klar, dass der null ist. musst schon den context mal was zuweisen
 
Zuletzt bearbeitet von einem Moderator:
T

tc-maxx

Neues Mitglied
Threadstarter
Hi,
ja das ist der richtige Code (heißt jetzt APNActivity)

Thx! Iich habe jetzt context = this zuweisen, nun gehts schon mal etwas weiter..

Code:
Caused by: java.lang.SecurityException: No permission to write APN settings: Neither user 10028 nor current process has android.permission.WRITE_APN_SETTINGS.       
 at android.os.Parcel.readException(Parcel.java:1218)                                                                                                         
 at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:160)                                                                            
 at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114)                                                                            
 at android.content.ContentProviderProxy.insert(ContentProviderNative.java:445)                                                                               
 at android.content.ContentResolver.insert(ContentResolver.java:629)                                                                                          
 at de.APN.APNActivity.InsertAPN(APNActivity.java:80)                                                                                                         
 at de.APN.APNActivity.onCreate(APNActivity.java:27)                                                                                                          
 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)                                                                               
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
aber warum habe ich keine Rechte die APN's zu setzen?
Dazu muss das Gerät doch nicht gerootet sein. :ohmy:

vg
 
S

swordi

Gewerbliches Mitglied
wie wärs mit google ?

wenn du deine fehlermeldungen mal kurz vorher bei google suchst, wirst mind. 95% deiner probleme auf anhieb finden.
 
T

tc-maxx

Neues Mitglied
Threadstarter
Ach stimmt, davon habe ich gelesen :unsure:

Jetzt funzt dat!
Vielen Dank euch :thumbsup:

vg
 
Oben Unten