I am working on Android 12 (AOSP 12) device. Trying to set the static IP from my application.
Snipped code below:
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private static final String KEY_ETH_IP_ADDRESS = "ethernet_ip_addr";
private static final String KEY_ETH_HW_ADDRESS = "ethernet_hw_addr";
private static final String KEY_ETH_NET_MASK = "ethernet_netmask";
private static final String KEY_ETH_GATEWAY = "ethernet_gateway";
private static final String KEY_ETH_DNS1 = "ethernet_dns1";
private static final String KEY_ETH_DNS2 = "ethernet_dns2";
private static final String KEY_ETH_MODE = "ethernet_mode_select";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (Settings.System.canWrite(this)) {
Toast.makeText(this, "Hello World", Toast.LENGTH_SHORT).show();
ContentResolver cr = this.getContentResolver();
Settings.System.putString(cr,KEY_ETH_IP_ADDRESS, "10.xxx.xxx.15");
Settings.System.putString(cr,KEY_ETH_NET_MASK, "255.255.255.100");
Settings.System.putString(cr,KEY_ETH_GATEWAY, "10.xx.xx.100");
Settings.System.putString(cr,KEY_ETH_DNS1, "10.xxx.xx.10");
Settings.System.putString(cr,KEY_ETH_DNS2, "10.xxx.xx.10");
} else {
Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse("package:" + this.getPackageName()));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
}
Given below permission in AndroidManifest.xml file :
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" tools:ignore="CoarseFineLocation" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS" tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" tools:ignore="ProtectedPermissions" />
Error:
java.lang.IllegalArgumentException: You cannot keep your settings in the secure settings.
at com.android.providers.settings.SettingsProvider.warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk(SettingsProvider.java:2266)
at com.android.providers.settings.SettingsProvider.enforceRestrictedSystemSettingsMutationForCallingPackage(SettingsProvider.java:2036)
at com.android.providers.settings.SettingsProvider.mutateSystemSetting(SettingsProvider.java:1889)
at com.android.providers.settings.SettingsProvider.insertSystemSetting(SettingsProvider.java:1840)
at com.android.providers.settings.SettingsProvider.call(SettingsProvider.java:462)
...
Process: com.xxx.test_staticip, PID: 3267
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxx.test_staticip/com.xxx.test_staticip.MainActivity}: java.lang.IllegalArgumentException: You cannot keep your settings in the secure settings.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3707)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3864)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2253)
...
Caused by: java.lang.IllegalArgumentException: You cannot keep your settings in the secure settings.
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:172)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:142)
at android.content.ContentProviderProxy.call(ContentProviderNative.java:732)
at android.provider.Settings$NameValueCache.putStringForUser(Settings.java:2867)
at android.provider.Settings$System.putStringForUser(Settings.java:3572)
at android.provider.Settings$System.putStringForUser(Settings.java:3556)
at android.provider.Settings$System.putString(Settings.java:3529)
at com.xxx.test_staticip.MainActivity.onCreate(MainActivity.java:84)
at android.app.Activity.performCreate(Activity.java:8072)
at android.app.Activity.performCreate(Activity.java:8052)
...
I couldn't found proper info on Google, some solutions are very old and those are deprecated in Android 12.