9

In my Android application I would like get all the available APNs and check if the client APN is available. I would like to run my app using this client APN.

Is there a way to achieve this on Android?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Remmyabhavan
  • 1,689
  • 5
  • 36
  • 65

3 Answers3

7

This might not answer your question directly. Have a look at this . Though keep in mind that this code is for reference only and should not be used in your app.

To get defined APN:

Cursor c = getContentResolver().query(Uri.withAppendedPath(Telephony.Carriers.CONTENT_URI, "current"), null, null, null, null);

And then refer to Telephony.Carriers for relevant columns.

Asahi
  • 13,378
  • 12
  • 67
  • 87
  • Ya i tried using this in my app and it throws this error: ` java.lang.SecurityException: No permission to write APN settings: Neither user 10099 nor current process has android.permission.WRITE_APN_SETTINGS.` – Etienne Lawlor Feb 24 '13 at 23:33
  • add required permission (http://developer.android.com/reference/android/Manifest.permission.html#WRITE_APN_SETTINGS) to the manifest – Asahi Feb 27 '13 at 11:29
  • When i tried adding it to the manifest it said only Native apps can add this permission. – Etienne Lawlor Feb 27 '13 at 17:45
  • So I cleaned the build after editing the AndroidManifest.xml file, so now it doesn't display any syntax errors. However, when it makes this query again it shows this error: `java.lang.SecurityException: No permission to write APN settings: Neither user 10099 nor current process has android.permission.WRITE_APN_SETTINGS.` – Etienne Lawlor Feb 28 '13 at 06:27
  • ahhh this is great http://stackoverflow.com/questions/13453640/read-apns-in-android-4-2 it looks like this can't be done. What is the alternative for all those application which uses mobile data to perform operations(like sending MMS) and can not get to know what is the default APN setting present in the device? – Etienne Lawlor Feb 28 '13 at 06:37
  • I guess i should note that I am testing this on a Nexus 4 which is running Android v4.2.2. – Etienne Lawlor Feb 28 '13 at 06:49
5

If you want to read the APN for Android 4.2 and more they are a change to do. I tested it, and it works.

In Android 4.1 and above use this:

Cursor c = getContentResolver().query(Uri.withAppendedPath(Telephony.Carriers.CONTENT_URI, "current"), null, null, null, null);

And for Android 4.2 and more use this code:

private static final String[] APN_PROJECTION = {
     Telephony.Carriers.TYPE,            // 0
     Telephony.Carriers.MMSC,            // 1
     Telephony.Carriers.MMSPROXY,        // 2
     Telephony.Carriers.MMSPORT          // 3
 };

And this line:

final Cursor apnCursor =SqliteWrapper.query(context, this.context.getContentResolver(), Uri.withAppendedPath(Carriers.CONTENT_URI, "current"), APN_PROJECTION, null, null, null);

The SQLiteWrapperClass is hidden (I found this class on the Internet).

import android.database.sqlite.SqliteWrapper;
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
vincent091
  • 2,325
  • 4
  • 17
  • 21
3

You will need the permission:

<uses-permission android:name="android.permission.WRITE_APN_SETTINGS" />

The code:

 private void addApn(Intent intent) {
        final String apn = intent.getStringExtra(APN_EXTRA_APN);
        final String name = intent.getStringExtra(APN_EXTRA_NAME);
        final String type = intent.getStringExtra(APN_EXTRA_TYPE);
        final String proxy = intent.getStringExtra(APN_EXTRA_PROXY);
        final int mnc = intent.getIntExtra(APN_EXTRA_MNC, 6);
        final int mcc = intent.getIntExtra(APN_EXTRA_MCC, 724);
        final String user = intent.getStringExtra(APN_EXTRA_USER);
        final String password = intent.getStringExtra(APN_EXTRA_PASSWORD);
        final String server = intent.getStringExtra(APN_EXTRA_SERVER);

        final ContentResolver cr = mContext.getContentResolver();
        ContentValues values = new ContentValues();
        values.put(Telephony.Carriers.APN, apn);
        values.put(Telephony.Carriers.NAME, name);
        values.put(Telephony.Carriers.TYPE, type);
        values.put(Telephony.Carriers.PROXY, proxy);
        values.put(Telephony.Carriers.MNC, mnc);
        values.put(Telephony.Carriers.MCC, mcc);
        values.put(Telephony.Carriers.USER, user);
        values.put(Telephony.Carriers.PASSWORD, password);
        values.put(Telephony.Carriers.SERVER, server);
        cr.insert(Telephony.Carriers.CONTENT_URI, values);
    }
Rodrigo Gontijo
  • 587
  • 1
  • 9
  • 21