Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Android Wearを判定する
import android.content.Context;
import java.lang.reflect.Method;
public class JudgeAndroidWear {
public static boolean isWear(Context context){
String defaultCharacteristics = "";
String characteristics = get(context, "", defaultCharacteristics);
return characteristics.contains("watch");
* from StackOverFlow
* 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
* @throws IllegalArgumentException if the key exceeds 32 characters
private static String get(Context context, String key, String def) throws IllegalArgumentException {
String ret= def;
ClassLoader cl = context.getClassLoader();
Class SystemProperties = cl.loadClass("android.os.SystemProperties");
//Parameters Types
Class[] paramTypes= new Class[2];
paramTypes[0]= String.class;
paramTypes[1]= String.class;
Method get = SystemProperties.getMethod("get", paramTypes);
Object[] params= new Object[2];
params[0]= new String(key);
params[1]= new String(def);
ret= (String) get.invoke(SystemProperties, params);
}catch( IllegalArgumentException iAE ){
throw iAE;
}catch( Exception e ){
ret= def;
return ret;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment