I have looked everywhere. We are using a Motorola Zoom to try our tablet site testing. The issue is that the Android Useragent is a general Useragent and there is no difference between tablet Android and mobile Android. I don't want to just target a specific device like the Xoom Useragent since Android will most likely be on multiple tablet devices in the near future.
We are using Umbraco CMS, and we have looked at using the 51Degrees.mobi solution, and at this moment this will not work for our needs. Maybe in the future. I know 51Degrees and some of the people at Umbraco are going to be doing some integration into Umbraco, but the final project will probably not be out for a couple of months.
Hence, why we would like to detect the Useragent String of an Android tablet and have it not direct to our mobile site like it currently is.
If anyone knows how to detect and Android Tablet in general, not the specific device would be very helpful.
The issue is that the Android User-Agent is a general User-Agent and there is no difference between tablet Android and mobile Android.
This is incorrect. Mobile Android has "Mobile" string in the User-Agent header. Tablet Android does not.
But it is worth mentioning that there are quite a few tablets that report "Mobile" Safari in the userAgent and the latter is not the only/solid way to differentiate between Mobile and Tablet.
@Carlos: In his article Tim Bray recommends this (as does another post by Google), but unfortunately it is not being applied by all tablet manufacturers.
... We recommend that manufactures of large-form-factor devices remove "Mobile" from the User Agent...
Most Android tablet user-agent strings I've seen use mobile safari, e.g. the Samsung Galaxy Tab:
Mozilla/5.0 (Linux; U; Android 2.2; en-us; SCH-I800 Build/FROYO) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
So at the moment I am checking on device names to detect Android tablets. As long as there are just a few models on the market, that's ok but soon this will be an ugly solution.
At least in case of the XOOM, the mobile part seems to be gone:
Mozilla/5.0 (Linux; U; Android 3.0; en-us; Xoom Build/HRI39) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13
But as there are currently only tablets with Andorid 3.x, checking on Android 3 would be enough.
Mo’ better to also detect “mobile” user-agent
While you may still want to detect “android” in the User-Agent to implement Android-specific features, such as touch-screen optimizations, our main message is: Should your mobile site depends on UA sniffing, please detect the strings “mobile” and “android,” rather than just “android,” in the User-Agent. This helps properly serve both your mobile and tablet visitors.
Detecting Android device via Browser
< script language="javascript"> <!--
var mobile = (/iphone|ipad|ipod|android|blackberry|mini|windows\sce|palm/i.test(navigator.userAgent.toLowerCase()));
if (mobile) {
alert("MOBILE DEVICE DETECTED");
document.write("<b>----------------------------------------<br>")
document.write("<b>" + navigator.userAgent + "<br>")
document.write("<b>----------------------------------------<br>")
var userAgent = navigator.userAgent.toLowerCase();
if ((userAgent.search("android") > -1) && (userAgent.search("mobile") > -1))
document.write("<b> ANDROID MOBILE <br>")
else if ((userAgent.search("android") > -1) && !(userAgent.search("mobile") > -1))
document.write("<b> ANDROID TABLET <br>")
}
else
alert("NO MOBILE DEVICE DETECTED"); //--> </script>
You can try this script out since you do not want to target the Xoom only. I don't have a Xoom, but should work.
function mobile_detect(mobile,tablet,mobile_redirect,tablet_redirect,debug) {
var ismobile = (/iphone|ipod|android|blackberry|opera|mini|windows\sce|palm|smartphone|iemobile/i.test(navigator.userAgent.toLowerCase()));
var istablet = (/ipad|android|android 3.0|xoom|sch-i800|playbook|tablet|kindle/i.test(navigator.userAgent.toLowerCase()));
if (debug == true) {
alert(navigator.userAgent);
}
if (ismobile && mobile==true) {
if (debug == true) {
alert("Mobile Browser");
}
window.location = mobile_redirect;
} else if (istablet && tablet==true) {
if (debug == true) {
alert("Tablet Browser");
}
window.location = tablet_redirect;
}
}
I created a project on github. Check it out - https://github.com/codefuze/js-mobile-tablet-redirect. Feel free to submit issues if there is anything wrong!
Once I have detected Android in the user agent, this is how I differentiate between tablet and smartphone browsers (this is using Python, but is similarly simple for other programming languages):
if ("Android" in agent):
if ("Mobile" in agent):
deviceType = "Phone"
else:
deviceType = "Tablet"
UPDATED: to reflect use of Chrome on Android, as per comments below.
Based on Agents strings on this site:
http://www.webapps-online.com/online-tools/user-agent-strings
This results came up:
First:
All Tablet Devices have:
1. Tablet
2. iPad
Second:
All Phone Devices have:
1. Mobile
2. Phone
Third:
Tablet and Phone Devices have:
1. Android
If you can detect level by level, I thing the result is 90 percent true. Like SharePoint Device Channels.
Here is what I use:
public static boolean onTablet()
{
int intScreenSize = getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;
return (intScreenSize == Configuration.SCREENLAYOUT_SIZE_LARGE) // LARGE
|| (intScreenSize == Configuration.SCREENLAYOUT_SIZE_LARGE + 1); // Configuration.SCREENLAYOUT_SIZE_XLARGE
}
While we can't say if some tablets omit "mobile", many including the Samsung Galaxy Tab do have mobile in their user-agent, making it impossible to detect between an android tablet and android phone without resorting to checking model specifics. This IMHO is a waste of time unless you plan on updating and expanding your device list on a monthly basis.
Unfortunately the best solution here is to complain to Google about this and get them to fix Chrome for Android so it adds some text to identify between a mobile device and a tablet. Hell even a single letter M OR T in a specific place in the string would be enough, but I guess that makes too much sense.
Xoom has the word Xoom in the user-agent: Mozilla/5.0 (Linux; U; Android 3.0.1; en-us; Xoom Build/HRI66) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13
Galaxy Tab has "Mobile" in the user-agent: Mozilla/5.0 (Linux; U; Android 2.2; en-us; SCH-I800 Build/FROYO) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
So, it's easy to detect the Xoom, hard to detect if a specific Android version is mobile or not.
Most modern tablets run honeycomb aka 3.x No phones run 3.x by default. Most tablets that currently run 2.x have less capacity and might be better of when presented with a mobile site anyway. I know it 's not flawless.. but I guess it 's a lot more accurate than the absence of mobile..
While Mobile Android may have "mobile" in it's user-agent string, what if it's using Opera Mobile for Android on a Tablet? It'll still have "mobile" in it's user-agent string, but should be displaying Tablet-sized sites. You'll need to test for "mobile" that is not preceded by "opera" rather than just "mobile"
or you could just forget about Opera Mobile.
I would recommend using Categorizr to detect if the user is on a tablet. You can view categorizr test results here.
If you use the absence of "Mobile" then its almost correct. But there are HTC Sensation 4G (4.3 inch with android 2.X) which does not send Mobile keyword.
The reason why you may want to treat it separately is due to iframes
etc.
The 51Degrees beta, 1.0.1.6 and the latest stable release 1.0.2.2 (4/28/2011) now have the ability to sniff for tablet. Basically along the lines of:
string capability = Request.Browser["is_tablet"];
Hope this helps you.
Try OpenDDR, it is free unlike most other solutions mentioned.
来源:https://stackoverflow.com/questions/5341637/how-do-detect-android-tablets-in-general-useragent