Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android emulator browser detecting

I'm developing a mobile version of a website. I'm currently using this Javascript to detect and redirect the user:

if((navigator.userAgent.match(/iPhone/i)) || 
                (navigator.userAgent.match(/Android/i)) ||
                (navigator.userAgent.match(/iPod/i))) 
        { 
        window.location = "http://sitename.com/m/";
    }

Works fine with iPhone and iPod, but no success with Android. I'm using the Android Emulator within Eclipse. I don't have an Android gadget to actually test it.

Am I doing something wrong? Anyone having the same issue?

like image 557
Vitor Avatar asked Feb 12 '10 19:02

Vitor


People also ask

Are Android emulators detectable?

There is no official API in iOS or Android to detect an emulator.

What does emulator Detected mean?

Emulator detection is the ability to tell when your application is running on an emulator rather than a real device, but why would you want to do this?

Is there a browser based Android Emulator?

BlueStacks is arguably the most popular Android emulator for its super-fast speed and lag-free gaming experience. The company has recently rolled out the public beta of BlueStacks X which is browser-based.


2 Answers

You should use location.replace instead of window.location

Example:

if( (navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/Android/i)) || (navigator.userAgent.match(/iPod/i)) ) { 
    location.replace("http://sitename.com/m/");
}

I used this code and it works on iphone/itouch and android phones/devices.

like image 135
JP. Avatar answered Sep 17 '22 12:09

JP.


Here is mine JavaScript function to detect Android device:

function isAndroid() {
    var ua = navigator.userAgent;
    return ua.match(/Android/) 
        || ua.match(/Dalvik/)
        || ua.match(/GINGERBREAD/)
        || ua.match(/Linux;.*Mobile Safari/)
        || ua.match(/Linux 1\..*AppleWebKit/)
};
like image 23
Vadim Avatar answered Sep 17 '22 12:09

Vadim