Tablet or Phone - Android

后端 未结 30 2539
难免孤独
难免孤独 2020-11-22 08:33

Is there a way to check if the user is using a tablet or a phone? I\'ve got problems with my tilt function and my new tablet (Transformer)

30条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-22 09:23

    I think a tablet has a min and max 600 px width and height,
    so need to know the screen density and the height/width in dp,
    to retrieve the value :

    DisplayMetrics metrics = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
    Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    int width = display.getWidth(); 
    int height = display.getHeight(); 
    float density = metrics.density;  
    if((width/density>=600 && height/density>=600))
     isTablette = true;
    else
     isTablette = false;
    

提交回复
热议问题