在我的android应用程序中,我想用android代码更改设备APN。但它给出了安全异常。我在Google上进行了很多搜索,但没有得到任何答案。如果有人知道此安全异常解决方案,请帮助我。
Intent intent = new Intent(android.provider.Settings.ACTION_APN_SETTINGS);
addApn(intent,MainActivity.this);
startActivityForResult(intent,1);
我使用这种方法在APN设置上发布了静态数据。
private void addApn(Intent intent, Context context) {
final String apn = intent.getStringExtra("www.airtelgprs.com");
final String name = intent.getStringExtra("airtel");
final String type = intent.getStringExtra("ope");
final String proxy = intent.getStringExtra("12.25.25");
final int mnc = intent.getIntExtra("mnc", 6);
final int mcc = intent.getIntExtra("mcc", 724);
final String user = intent.getStringExtra("SAnjeev");
final String password = intent.getStringExtra("123456789");
final String server = intent.getStringExtra("mera server");
final ContentResolver cr = context.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);
}
<uses-permission android:name="android.permission.WRITE_APN_SETTINGS"
tools:ignore="ProtectedPermissions" />
java.lang.SecurityException: No permission to write APN settings
at android.os.Parcel.readException(Parcel.java:1620)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
at android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
at android.content.ContentResolver.query(ContentResolver.java:493)
at android.content.ContentResolver.query(ContentResolver.java:435)
答案 0 :(得分:0)
也许您没有在运行时获得许可。试试这个:
@Override
protected void onResume() {
super.onResume();
int storagePermissionGranted = ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_APN_SETTINGS);
if (storagePermissionGranted == PackageManager.PERMISSION_GRANTED) {
// Do logic
} else {
PermissionUtils.checkPermission(this, Manifest.permission.WRITE_APN_SETTINGS, 1);
}
}
别忘了在清单上添加权限:
<uses-permission android:name="android.permission.WRITE_APN_SETTINGS" />
答案 1 :(得分:0)
APN写权限仅授予系统应用程序。您由于权限而面临此错误。
答案 2 :(得分:0)
根据文档,此权限未授予第三方应用程序。
https://developer.android.com/reference/android/Manifest.permission#WRITE_APN_SETTINGS
答案 3 :(得分:0)
我解决了这个问题并在 Android 7.1.1 上进行了测试。
您必须将您的程序转换为系统应用程序,因此请按照以下步骤操作:
1- Generate App.apk
2- run emulator as writable-system(emulator file exist in SDK_HOME/tools)
.\emulator.exe -avd {EMULATOR NAME} -writable-system
3- to see list of emulator name run this command
.\emulator.exe -list-avds
4- run adb shell as root
adb root
adb shell
5- run this command
mount -o rw,remount /system
6- copy App.apk to /system/app and /system/priv-app
7- reboot emulator