使用JavaScript或PHP进行Android检测

I've noticed that two of my blog posts continue to get more popular each week:  iPad Detection with JavaScript or PHP and iPhone and iPad detection with JavaScript or PHP. What's obvious is that Android development is a hot topic that will only grow.  Here are a few methods by which you can detect iOS' main competitor:  Android.

我注意到,我的两个博客文章继续每周越来越受欢迎: 使用JavaScript或PHPiPhone进行iPad检测以及使用JavaScript或PHP进行iPad检测 。 显而易见,Android开发是一个热门话题,并且只会不断增长。 您可以通过以下几种方法来检测iOS的主要竞争对手:Android。

JavaScript (The JavaScript)

Searching the user agent string for "Android" is the quickest method:

在用户代理字符串中搜索“ Android”是最快的方法:


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 (The PHP)

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

同样,我们将使用PHP的strstr函数在用户代理中搜索Android:


$ua = strtolower($_SERVER['HTTP_USER_AGENT']);
if(stripos($ua,'android') !== false) { // && stripos($ua,'mobile') !== false) {
	header('Location: http://android.davidwalsh.name');
	exit();
}


奖金! .htaccess检测 (Bonus!  .htaccess Detection)

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

我们甚至可以使用.htaccess指令来检测Android设备并对其做出React!


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


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

一切就绪:三种不同的Android设备检测! 享受您的移动开发乐趣!

翻译自: https://davidwalsh.name/detect-android

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值