How to check weather the device is iPad and tablet in phonegap

前端 未结 3 2029
生来不讨喜
生来不讨喜 2020-12-20 02:25

How to check device in phonegap?.Actually i need that my application is only run only tablet or in Iphone .I need to block my application on other phones only run Android ta

3条回答
  •  春和景丽
    2020-12-20 03:11

    Cordova "out-of-the-box" can't determine if a device is a tablet.

    For iOS, a simple bit of Javascript can be used to detect if the device is an iPad by examining the user agent string:

    var isTablet = !!navigator.userAgent.match(/iPad/i);
    

    However, for Android, JS isn't good enough, it requires some native Java:

    private boolean isTabletDevice(Context applicationContext) {
            boolean device_large = ((applicationContext.getResources().getConfiguration().screenLayout &
                    Configuration.SCREENLAYOUT_SIZE_MASK) >=
                    Configuration.SCREENLAYOUT_SIZE_LARGE);
    
            if (device_large) {
                DisplayMetrics metrics = new DisplayMetrics();
                Activity activity = this.cordova.getActivity();
                activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
    
                if (metrics.densityDpi == DisplayMetrics.DENSITY_DEFAULT
                        || metrics.densityDpi == DisplayMetrics.DENSITY_HIGH
                        || metrics.densityDpi == DisplayMetrics.DENSITY_MEDIUM
                        || metrics.densityDpi == DisplayMetrics.DENSITY_TV
                        || metrics.densityDpi == DisplayMetrics.DENSITY_XHIGH) {
                    Log.d(LOG_TAG, "Is Tablet Device");
                    return true;
                }
            }
            Log.d(LOG_TAG, "Is NOT Tablet Device");
            return false;
    }
    

    This plugin wraps up both these approaches in an easy-to-use package for Android and iOS: https://github.com/dpa99c/phonegap-istablet.

提交回复
热议问题