in my application i have this button btn3GOn that if i tap it will enable the data connection, i have read and search for some clues but i fail but the method was the best answer. now when i tap the button it doesnt turn on the data connection, i am using jellybean API.
btn3GOon.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
setMobileDataEnabled(null, false);
} catch(Exception e){
e.printStackTrace();
}
}
});
}
private void setMobileDataEnabled(Context context, boolean enabled) throws Exception {
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);
}