Reputation: 1528
In my app i want to navigate user to settings where he/she able to activate device mobile data.
I want to use intent to solve this but my problem is that mobile data option is in different pages in device settings for Exmaple below code work fine in Sony Xperia Z2 device (with android 5.1.1)
startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
But in some other device such as sony experia SP (android 4.3) and samsung S4 (android 5.0.1) user must choose one more step and go to mobile network page. I can resolve this problem with this code : (Going straight to mobile network)
Intent intent=new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);
ComponentName cn = new ComponentName("com.android.phone","com.android.phone.Settings");
intent.setComponent(cn);
startActivity(intent);
But user with z2 must return one step back ! How can i resolve this ? choose which way ? (I hope explain problem clear )
Upvotes: 0
Views: 1054
Reputation: 3873
try this way:
Also add the permission
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
method to enable mobile/3g data:
private void setMobileDataEnabled(Context context, boolean enabled) {
final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
final Class conmanClass = Class.forName(conman.getClass().getName());
final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
iConnectivityManagerField.setAccessible(true);
final Object iConnectivityManager = iConnectivityManagerField.get(conman);
final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
setMobileDataEnabledMethod.setAccessible(true);
setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);
}
For lollipop and above:
The setMobileDataEnabled method is no longer callable as of Android L and later
Upvotes: 1