问题
I need to be able to read current APN name. My app is a system app (it's located under /system/app) and I have root access.
I'm trying to get APN name but it's being impossible because I'm always prompted with:
No permission to write APN settings
I also have added the following permissions in Android Manifest
<uses-permission android:name="android.permission.WRITE_APN_SETTINGS"/>
TARGET SDK > 18 (Lollipop)
Thanks a lot.
回答1:
Android 5.1 introduced Carrier Privileges (https://source.android.com/devices/tech/config/uicc).
To be possible to change the APN you have to sign your app with the same signature of the SIM card. If you do this, then it is possible to change the APN. You do not need the
<uses-permission android:name="android.permission.WRITE_APN_SETTINGS"/>
in Android Manifest.
You can sign your app with Android Studio for example (https://developer.android.com/studio/publish/app-signing.html)
回答2:
WRITE_APN_SETTINGS
Not for use by third-party applications.
You cant read the APN settings in API > 18
.
For API <= 18
public class APNHelper {
private Context context;
public APNHelper(final Context context) {
this.context = context;
}
@SuppressWarnings("unchecked")
public List<APN> getMMSApns() {
final Cursor apnCursor = this.context.getContentResolver()
.query(Uri.withAppendedPath(Telephony.Carriers.CONTENT_URI,
"current"), null, null, null, null);
if (apnCursor == null) {
return Collections.EMPTY_LIST;
} else {
final List<APN> results = new ArrayList<APN>();
if (apnCursor.moveToFirst()) {
do {
final String type = apnCursor.getString(apnCursor
.getColumnIndex(Telephony.Carriers.TYPE));
if (!TextUtils.isEmpty(type)
&& (type.equalsIgnoreCase("*") || type
.equalsIgnoreCase("mms"))) {
final String mmsc = apnCursor.getString(apnCursor
.getColumnIndex(Telephony.Carriers.MMSC));
final String mmsProxy = apnCursor.getString(apnCursor
.getColumnIndex(Telephony.Carriers.MMSPROXY));
final String port = apnCursor.getString(apnCursor
.getColumnIndex(Telephony.Carriers.MMSPORT));
final APN apn = new APN();
apn.MMSCenterUrl = mmsc;
apn.MMSProxy = mmsProxy;
apn.MMSPort = port;
results.add(apn);
Toast.makeText(context,
mmsc + " " + mmsProxy + " " + port,
Toast.LENGTH_LONG).show();
}
} while (apnCursor.moveToNext());
}
apnCursor.close();
return results;
}
}
}
来源:https://stackoverflow.com/questions/37343094/read-current-apn-name-from-code-root