Install APN programmatically on Android

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

Is there any way to achieve this on Android?

+5
source share
3 answers

This may not answer your question directly. Take a look. Although keep in mind that this code is for reference only and should not be used in your application.

To get a specific APN:

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

And then refer to Telephony.Carriers for the relevant columns.

+4
source

If you want to read APN for Android 4.2 and more, this is a change. I tested it and it works.

On 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); 

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

 import android.database.sqlite.SqliteWrapper; 
+3
source

You will need permission:

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

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); } 
0
source

Source: https://habr.com/ru/post/944860/


All Articles