Program Club

장치를 감지하는 방법은 Android 전화 또는 Android 태블릿입니까?

proclub 2021. 1. 5. 08:21
반응형

장치를 감지하는 방법은 Android 전화 또는 Android 태블릿입니까?


Android 태블릿과 Android 휴대폰 용 앱이 두 개 있습니다. 태블릿 앱의 경우 android:minSdkVersion="11". 하지만 요즘 갤럭시 S3와 같은 안드로이드 폰에는 안드로이드 버전 4.0.4가 있으므로 S3 사용자는 구글 플레이 스토어에서 내 태블릿 앱을 다운로드 할 수 있습니다. 전화 사용자가 태블릿 앱을 설치할 때 전화 앱을 다운로드하도록 경고하고 싶습니다. 태블릿 사용자가 전화 앱을 실행할 때 태블릿 앱을 다운로드하는 경우도 마찬가지입니다.

장치 유형을 쉽게 감지 할 수있는 방법이 있습니까?

편집하다:

링크 에서 해결책을 찾았습니다 .

매니페스트 파일에서 핸드셋 및 태블릿의 화면 기능을 선언하면 Google Play가 휴대폰과 태블릿 모두에 대한 다운로드 권한을 결정합니다.


이것을 사용하십시오 :

public static boolean isTablet(Context context) {
    return (context.getResources().getConfiguration().screenLayout
            & Configuration.SCREENLAYOUT_SIZE_MASK)
            >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}

장치가 큰 화면에서 작동하는 경우 true를 반환합니다.

다른 유용한 방법은 여기에서 찾을 수 있습니다 .


리소스 파일 에서이
Add boolean 매개 변수를 시도 할 수도 있습니다.
res / values ​​/ dimen.xml 파일에이 줄을 추가하십시오.

<bool name="isTab">false</bool>

res / values-sw600dp / dimen.xml 파일에서 다음 줄을 추가합니다.

<bool name="isTab">true</bool>

그런 다음 Java 파일에서 다음 값을 얻습니다.

if(getResources().getBoolean(R.bool.isTab)) {
    System.out.println("tablet");
} else {
    System.out.println("mobile");
}

이 코드 스 니펫은 기기 유형이 7 인치 이상이고 Mdpi 이상 해상도인지 여부를 알려줍니다. 필요에 따라 구현을 변경할 수 있습니다.

 private static boolean isTabletDevice(Context activityContext) {
        boolean device_large = ((activityContext.getResources().getConfiguration().screenLayout &
                Configuration.SCREENLAYOUT_SIZE_MASK) ==
                Configuration.SCREENLAYOUT_SIZE_LARGE);

        if (device_large) {
            DisplayMetrics metrics = new DisplayMetrics();
            Activity activity = (Activity) activityContext;
            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) {
                AppInstance.getLogger().logD("DeviceHelper","IsTabletDevice-True");
                return true;
            }
        }
        AppInstance.getLogger().logD("DeviceHelper","IsTabletDevice-False");
        return false;
    }

나는 이것이 무언가가 전화를 걸 수 있는지 감지해야한다고 생각한다. 다른 모든 것은 전화 기능이없는 태블릿 / TV 일 것이다.

내가 본 한 이것은 화면 크기에 의존하지 않는 유일한 것입니다.

public static boolean isTelephone(){
    //pseudocode, don't have the copy and paste here right know and can't remember every line
    return new Intent(ACTION_DIAL).resolveActivity() != null;
}

Google Play 스토어 기능을 사용하고 태블릿에 태블릿 앱을 다운로드하고 휴대 전화에 전화 앱을 다운로드하도록 설정합니다.

사용자가 잘못된 앱을 설치했다면 다른 방법을 사용하여 설치했을 것입니다.


We had the similar issue with our app that should switch based on the device type - Tab/Phone. IOS gave us the device type perfectly but the same idea wasn't working with Android. the resolution/ DPI method failed with small res tabs, high res phones. after a lot of torn hairs and banging our heads over the wall, we tried a weird idea and it worked exceptionally well that won't depend on the resolutions. this should help you too.

in the Main class, write this and you should get your device type as null for TAB and mobile for Phone.

String ua=new WebView(this).getSettings().getUserAgentString();


if(ua.contains("Mobile")){
   //Your code for Mobile
}else{
   //Your code for TAB            
}

Use following code to identify device type.

private boolean isTabletDevice() {
     if (android.os.Build.VERSION.SDK_INT >= 11) { // honeycomb
         // test screen size, use reflection because isLayoutSizeAtLeast is only available since 11
         Configuration con = getResources().getConfiguration();
         try {
              Method mIsLayoutSizeAtLeast = con.getClass().getMethod("isLayoutSizeAtLeast");
              Boolean r = (Boolean) mIsLayoutSizeAtLeast.invoke(con, 0x00000004); // Configuration.SCREENLAYOUT_SIZE_XLARGE
              return r;
         } catch (Exception x) {
              return false;
         }
      }
    return false;
}

If you want to decide device is tablet or phone based on screen inch, you can use following

device 6.5 inches or higher consider as tablet, but some recent handheld phone has higher diagonal value. good thing with following solution you can set the margin.

public boolean isDeviceTablet(){
    DisplayMetrics metrics = new DisplayMetrics();
    this.getWindowManager().getDefaultDisplay().getMetrics(metrics);
    float yInches= metrics.heightPixels/metrics.ydpi;
    float xInches= metrics.widthPixels/metrics.xdpi;
    double diagonalInches = Math.sqrt(xInches*xInches + yInches*yInches);
    if (diagonalInches>=6.5) {
        return true;
    }
    return false;
}

ReferenceURL : https://stackoverflow.com/questions/11330363/how-to-detect-device-is-android-phone-or-android-tablet

반응형