2014年10月29日星期三

判断虚拟按键是否存在

不用考虑emulator的话,直接用hasPermanentMenuKey就可以了.
    
    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    private boolean hasNavBar(Context context) {
 Resources res = context.getResources();
 int resourceId = res.getIdentifier("config_showNavigationBar", 
             "bool","android");
 if (resourceId != 0) {
     boolean hasNav = res.getBoolean(resourceId);
     String sNavBarOverride = null;
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
     try {
  Class c = Class.forName("android.os.SystemProperties");
  Method m = c.getDeclaredMethod("get", String.class);
  m.setAccessible(true);
  sNavBarOverride = (String) m.invoke(null,"qemu.hw.mainkeys");
  } catch (Throwable e) {
     sNavBarOverride = null;
     }
  }

  // check override flag (see static block)
  if ("1".equals(sNavBarOverride)) {
      hasNav = false;
  } else if ("0".equals(sNavBarOverride)) {
      hasNav = true;
  }
      return hasNav;
  } else { // fallback
             return !ViewConfiguration.get(context).hasPermanentMenuKey();
  }
 }

没有评论:

发表评论