7

I want to create APN by code, is there any support in Android SDK, i have tried a lot but not succeed,I found some info related to this http://blogs.msdn.com/b/zhengpei/archive/2009/10/13/managing-apn-data-in-google-android.aspx i made a class using this reference but not able to do anything,can any please give the solution for this???? Thanks

Sandeep
  • 2,573
  • 3
  • 21
  • 28

2 Answers2

13

I will give some examples:

Getting default APN information:

//path to APN table
final Uri APN_TABLE_URI = Uri.parse("content://telephony/carriers");

//path to preffered APNs
final Uri PREFERRED_APN_URI = Uri.parse("content://telephony/carriers/preferapn");

//receiving cursor to preffered APN table
Cursor c = getContentResolver().query(PREFERRED_APN_URI, null, null, null, null);

//moving the cursor to beggining of the table
c.moveToFirst();

//now the cursor points to the first preffered APN and we can get some
//information about it
//for example first preffered APN id    
int index = c.getColumnIndex("_id");    //getting index of required column
Short id = c.getShort(index);           //getting APN's id from

//we can get APN name by the same way
index = c.getColumnIndex("name");
String name = c.getString(index); 

//and any other APN properties: numeric, mcc, mnc, apn, user, server,
//password, proxy, port, mmsproxy, mmsport, mmsc, type, current

To define new APN:

//first we have to create a new row in APN table
int id = -1;
ContentResolver resolver = this.getContentResolver();
ContentValues values = new ContentValues();

//create value, you can define any other APN properties in the same way
values.put("name", "Your APN Name");    //choose APN name, like 3G Orange
values.put("apn", "Your APN address");  //choose APN address, like cellcom.wapu.co.il

//now we have to define APN setting page UI. You have to get operator numeric property
//you can obtain it from TelephonyManager.getNetworkOperator() method
values.put("mcc", "your operator numeric high part");  //for example 242
values.put("mnc", "your operator numeric low part");   //for example 501
values.put("numeric", "your operator numeric");        //for example 242501

Cursor c = null;
try
{
    //insert new row to APN table
    Uri newRow = resolver.insert(APN_TABLE_URI, values);
    if(newRow != null)
    {
        c = resolver.query(newRow, null, null, null, null);

        //obtain the APN id
        int index = c.getColumnIndex("_id");
        c.moveToFirst();
        id = c.getShort(index);
    }
}
catch(Exception e)
{
}

//now after we created a new APN in APN table
//and APN's ID stored in id variable (or -1 if any troubles was happaned)
//we can define a new APN as default
values = new ContentValues();
values.put("apn_id", id); 

try
{
    resolver.update(PREFERRED_APN_URI, values, null, null);
}
catch (Exception e)
{
}

so, it have to work, but if it not - tell me and I will try to examine problems.

Borg8
  • 1,562
  • 11
  • 19
  • it seems that your example is what I am looking for, I tried use your code with 2.2 ANDROID **emulator**, First I Added WRITE_APN_SETTINGS to the Manifest file and then tried it, The first part of displaying the APN work great. But When I tried to create my own APN I am not getting any exception but I can't see the new APN. What do I missed ? Thanks – ZoharAdar Nov 24 '11 at 13:02
  • @ZoharAdar: I think you define wrong mcc, mnc and numeric values. They are responsible for showing APN on UI page. You can obtain proper values by using TelephonyManager.getNetworkOperator() method. numeric value you get directly from that method (for example: numeric = 123210), then extract mcc and mnc value from it (in our example: mcc = 123, mnc = 210). – Borg8 Nov 25 '11 at 15:10
  • 3
    In Android 4 third party applications can no longer receive WRITE_APN_SETTINGS permission. So you will get security exception every time when you try add, remove or edit APN. – Borg8 Jun 28 '12 at 19:19
  • @Borg8 What is the operator numeric? – Karthik Balakrishnan Aug 05 '13 at 16:20
  • numeric is a concatination of mcc and mnc of your cellular operator. Here you can found list of cellular operators code: http://en.wikipedia.org/wiki/Mobile_country_code For example, for operator Orange Israel: mcc = 425, mnc = 01 so numeric = 42501. – Borg8 Aug 07 '13 at 15:07
2

@Borg8 Thanks, you helped me a lot, this is what I missed, at first I could not see the new APN at the UI list. I found my answer at @DeepSan above link here.

To see the new APN that I just created in the ** emaltor ** UI, I use the 310260 numric number

// TelephonyProperties;
    values.put("mcc", "310");
    values.put("mnc", "260");
    values.put("numeric", "310260");

To See It on my Device(Galaxy) I Used the TelephonyManager:

  TelephonyManager tel = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
    String networkOperator = tel.getNetworkOperator();
    int mcc = 0;
    int mnc = 0;
    if (networkOperator != null) {
          mcc = Integer.parseInt(networkOperator.substring(0, 3));
          mnc = Integer.parseInt(networkOperator.substring(3));
    }

   // TelephonyProperties;
    values.put("mcc", mcc );
    values.put("mnc", mnc );
    values.put("numeric",networkOperator);

Now I can see the new APN on the UI.

ZoharAdar
  • 474
  • 1
  • 7
  • 19