Here are a few methods by which you can detect Android browser.

1.The PHP

Again, we’ll use PHP’s strstr function to search for Android in the user agent:

[php toolbar=”true” classname=”one_line”] $ua = strtolower($_SERVER[‘HTTP_USER_AGENT’]);
if(stripos($ua,’android’) !== false) { // && stripos($ua,’mobile’) !== false) {
header(‘Location: http://android.davidwalsh.name’);
exit();
}
[/php]

2.The JavaScript

Searching the user agent string for “Android” is the quickest method:

[php toolbar=”true” classname=”one_line”] var ua = navigator.userAgent.toLowerCase();
var isAndroid = ua.indexOf("android") > -1; //&& ua.indexOf("mobile");
if(isAndroid) {
// Do something!
// Redirect to Android-site?
window.location = ‘http://android.davidwalsh.name’;
}
[/php]

3.Bonus! .htaccess Detection

We can even use .htaccess directives to detect and react to Android devices!

[php toolbar=”true” classname=”one_line”] RewriteCond %{HTTP_USER_AGENT} ^.*Android.*$
RewriteRule ^(.*)$ http://android.davidwalsh.name [R=301] [/php]

And there you have it: three different Android device detection! Have fun with your mobile development!

Leave A Comment