Skip to content

Instantly share code, notes, and snippets.

@itstartstosnow
Created January 6, 2022 07:40
Show Gist options
  • Save itstartstosnow/b4a43614179415217b2d3299477bf695 to your computer and use it in GitHub Desktop.
Save itstartstosnow/b4a43614179415217b2d3299477bf695 to your computer and use it in GitHub Desktop.
Get/set Android SystemProperties
package com.sylvia.gist;
import android.util.Log;
import java.lang.reflect.Method;
public class SysPropProxy {
private static final String TAG = "SysPropProxy";
/**
* This class cannot be instantiated
*/
private SysPropProxy() {
}
/**
* Get the value for the given key.
*
* @return an empty string if the key isn't found
*/
public static String get(String key) {
String ret = "";
try {
Class<?> SystemProperties = Class.forName("android.os.SystemProperties");
//Parameters Types
@SuppressWarnings("rawtypes")
Class[] paramTypes = { String.class };
Method get = SystemProperties.getMethod("get", paramTypes);
//Parameters
Object[] params = { key };
ret = (String) get.invoke(SystemProperties, params);
} catch (IllegalArgumentException iAE) {
Log.e(TAG, Log.getStackTraceString(iAE));
} catch (Exception e) {
ret = "";
Log.e(TAG, Log.getStackTraceString(e));
}
return ret;
}
/**
* Get the value for the given key.
*
* @return if the key isn't found, return def if it isn't null, or an empty
* string otherwise
*/
public static String get(String key, String def) {
String ret = def;
try {
Class<?> SystemProperties = Class.forName("android.os.SystemProperties");
//Parameters Types
@SuppressWarnings("rawtypes")
Class[] paramTypes = { String.class, String.class };
Method get = SystemProperties.getMethod("get", paramTypes);
//Parameters
Object[] params = { key, def };
ret = (String) get.invoke(SystemProperties, params);
} catch (IllegalArgumentException iAE) {
Log.e(TAG, Log.getStackTraceString(iAE));
} catch (Exception e) {
ret = def;
Log.e(TAG, Log.getStackTraceString(e));
}
return ret;
}
/**
* Get the value for the given key, and return as an integer.
*
* @param key the key to lookup
* @param def a default value to return
* @return the key parsed as an integer, or def if the key isn't found or
* cannot be parsed
*/
public static Integer getInt(String key, int def) {
Integer ret = def;
try {
Class<?> SystemProperties = Class.forName("android.os.SystemProperties");
//Parameters Types
@SuppressWarnings("rawtypes")
Class[] paramTypes = { String.class, int.class };
Method getInt = SystemProperties.getMethod("getInt", paramTypes);
//Parameters
Object[] params = { key, def };
ret = (Integer) getInt.invoke(SystemProperties, params);
} catch (IllegalArgumentException iAE) {
Log.e(TAG, Log.getStackTraceString(iAE));
} catch (Exception e) {
ret = def;
Log.e(TAG, Log.getStackTraceString(e));
}
return ret;
}
/**
* Get the value for the given key, and return as a long.
*
* @param key the key to lookup
* @param def a default value to return
* @return the key parsed as a long, or def if the key isn't found or cannot
* be parsed
*/
public static Long getLong(String key, long def) {
Long ret = def;
try {
Class<?> SystemProperties = Class.forName("android.os.SystemProperties");
//Parameters Types
@SuppressWarnings("rawtypes")
Class[] paramTypes = { String.class, long.class };
Method getLong = SystemProperties.getMethod("getLong", paramTypes);
//Parameters
Object[] params = { key, def };
ret = (Long) getLong.invoke(SystemProperties, params);
} catch (IllegalArgumentException iAE) {
Log.e(TAG, Log.getStackTraceString(iAE));
} catch (Exception e) {
ret = def;
Log.e(TAG, Log.getStackTraceString(e));
}
return ret;
}
/**
* Get the value for the given key, returned as a boolean. Values 'n', 'no',
* '0', 'false' or 'off' are considered false. Values 'y', 'yes', '1', 'true'
* or 'on' are considered true. (case insensitive). If the key does not exist,
* or has any other value, then the default result is returned.
*
* @param key the key to lookup
* @param def a default value to return
* @return the key parsed as a boolean, or def if the key isn't found or is
* not able to be parsed as a boolean.
*/
public static Boolean getBoolean(String key, boolean def) {
Boolean ret = def;
try {
Class<?> SystemProperties = Class.forName("android.os.SystemProperties");
//Parameters Types
@SuppressWarnings("rawtypes")
Class[] paramTypes = { String.class, boolean.class };
Method getBoolean = SystemProperties.getMethod("getBoolean", paramTypes);
//Parameters
Object[] params = { key, def };
ret = (Boolean) getBoolean.invoke(SystemProperties, params);
} catch (IllegalArgumentException iAE) {
Log.e(TAG, Log.getStackTraceString(iAE));
} catch (Exception e) {
ret = def;
Log.e(TAG, Log.getStackTraceString(e));
}
return ret;
}
/**
* Set the value for the given key.
*/
public static void set(String key, String val) {
try {
Class<?> SystemProperties = Class.forName("android.os.SystemProperties");
//Parameters Types
@SuppressWarnings("rawtypes")
Class[] paramTypes = { String.class, String.class };
Method set = SystemProperties.getMethod("set", paramTypes);
//Parameters
Object[] params = { key, val };
set.invoke(SystemProperties, params);
} catch (IllegalArgumentException iAE) {
Log.e(TAG, Log.getStackTraceString(iAE));
} catch (Exception e) {
Log.e(TAG, Log.getStackTraceString(e));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment