Asked  7 Months ago    Answers:  5   Viewed   49 times

I would like to get info about a device to see if it's a smartphone or tablet. How can I do it?

I would like to show different web pages from resources based on the type of device:

String s="Debug-infos:";
s += "n OS Version: " + System.getProperty("os.version") + "(" +    android.os.Build.VERSION.INCREMENTAL + ")";
s += "n OS API Level: " + android.os.Build.VERSION.SDK;
s += "n Device: " + android.os.Build.DEVICE;
s += "n Model (and Product): " + android.os.Build.MODEL + " ("+ android.os.Build.PRODUCT + ")";

However, it seems useless for my case.


This solution works for me now:

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;

if (SharedCode.width > 1023 || SharedCode.height > 1023){
   //code for big screen (like tablet)
}else{
   //code for small screen (like smartphone)
}

 Answers

49

This subject is discussed in the Android Training:

Use the Smallest-width Qualifier

If you read the entire topic, they explain how to set a boolean value in a specific value file (as res/values-sw600dp/attrs.xml):

<resources>
    <bool name="isTablet">true</bool>
</resources>

Because the sw600dp qualifier is only valid for platforms above android 3.2. If you want to make sure this technique works on all platforms (before 3.2), create the same file in res/values-xlarge folder:

<resources>
    <bool name="isTablet">true</bool>
</resources>

Then, in the "standard" value file (as res/values/attrs.xml), you set the boolean to false:

<resources>
    <bool name="isTablet">false</bool>
</resources>

Then in you activity, you can get this value and check if you are running in a tablet size device:

boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
if (tabletSize) {
    // do something
} else {
    // do something else
}
Tuesday, June 1, 2021
 
SpiderLinked
answered 7 Months ago
40

You can use the following JS function:

function isTouchDevice() {
   var el = document.createElement('div');
   el.setAttribute('ongesturestart', 'return;'); // or try "ontouchstart"
   return typeof el.ongesturestart === "function";
}

Source: Detecting touch-based browsing.

Please note the above code only tests if the browser has support for touch, not the device.

Related links:

  • Optimize website for touch devices
  • What is the best way to detect a mobile device in jQuery?
  • What's the best way to detect a 'touch screen' device using JavaScript?
  • Mozilla.org Detecting touch: it’s the ‘why’, not the ‘how’

There may be detection in jquery for mobile and jtouch

Friday, June 18, 2021
 
jab
answered 6 Months ago
jab
99

I wrote an article for MSDN that never got published, I guess because Tablet PC development fizzled out by the time I got it to them. But it described how to do this. Long story short, you'll want the GetMessageExtraInfo API. Here's the definitions:

// [DllImport( "user32.dll" )]
// private static extern uint GetMessageExtraInfo( );

uint extra = GetMessageExtraInfo();
bool isPen = ( ( extra &  0xFFFFFF00 ) == 0xFF515700 );

Email me at my first name at Einstein Tech dot net if you want me to send you the article.

Friday, August 13, 2021
 
Saurabh
answered 4 Months ago
10

I always run adb as root the first time, and as normal user thereafter. But I run a virtual machine that I suspend so I don't have to do it often.

ou have the right idea, the solution is most definately that you need the permission in the file within rules directory. The only suggestion I can make is that you are using the wrong vendor id.

Look it up here

Also, to make sure you're running the correct adb under root, run both of:

sudo adb kill-server
adb kill-server

before:

sudo adb start-server

I have found just running the one doesn't always work.

Sunday, August 15, 2021
 
nfechner
answered 4 Months ago
94

void onPasswordFailed(Context context, Intent intent, UserHandle user) only works with Oreo and above.

For backward compatibility, you also need to override onPasswordFailed(Context context, Intent intent). That will make your code work on Nougat and below.

Tuesday, August 24, 2021
 
IcedAnt
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share