Detecting a Mobile Device



Your application may need to function slightly different based on the type of device the user has.  With that in mind, the ability to detect the device type in your program can be very useful.  Fortunately, this can be accomplished by simply interrogating the User Agent property or environment variable.  The following examples illustrate how to detect an Android device using different languages.  Other device types can be detected in a similar manner.

Using JavaScript Code:
var useragent = navigator.userAgent; if (useragent.match(/Android/i)) { // This is an Android device }
Using RPG Code:
D getenv PR * ExtProc('getenv') D name * Value Options(*String) D useragent s 250A /Free useragent = %str(getenv('HTTP_USER_AGENT')); If %scan('Android' : useragent) > 0; // This is an Android device EndIf; /End-Free
Using PHP Code:
$useragent = $_SERVER['HTTP_USER_AGENT']; if (stripos($useragent, 'Android') !== false) { // This is an Android device }