Reputation: 81
In my application, I used a snippet code below
public static void setMobileDataEnabled(Context context, boolean enabled) {
try {
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);
} catch (Exception e) {
e.printStackTrace();
}
Can anyone know when I used this code in my application, Google still allows me to upload my app, or will prevent/reject - because it is private API / forbidden API?
Upvotes: 1
Views: 976
Reputation: 61
Yes, it is supported, and even recommended in the situation where you want compatibility with multiple versions of the Android OS in one apk file.
You can check the article from android official blog about reflection.
http://android-developers.blogspot.com.br/2009/04/backward-compatibility-for-android.html
Upvotes: 1