简体   繁体   English

如何检测是否使用Android Browser或Android Native WebView Client

[英]How to detect if using Android Browser or Android Native WebView Client

At first i will explain what i have, and then what i want to do. 起初我会解释我有什么,然后我想做什么。 I've been searching a lot and you can think that this question already exists, but i think is a very specific case. 我一直在搜索,你可以认为这个问题已经存在,但我认为这是一个非常具体的案例。

I have a web application, using a template system with php,jQuery,javascript, etc... Now what i'm doing is developing a mobile web version, with specific templates for mobile devices. 我有一个Web应用程序,使用模板系统与php,jQuery,javascript等...现在我正在做的是开发一个移动网络版本,具有移动设备的特定模板。 It works fine but the problem comes when I wanted to include the mobile web in android native app , inside a WebView . 它工作正常,但问题来自我想在Android本机应用程序中包含移动Web,在WebView中

I have enabled javascript and everything needed to make it work, in order to use scripts for calling my own Android functions. 我已经启用了javascript以及使其工作所需的一切,以便使用脚本来调用我自己的Android功能。 And it works great! 而且效果很棒!

The problem is that when I access from any browser from outside the app, the mobile web and Android detects the device, but it says that the functions are not defined when I call functions android native app like this: 问题是,当我从应用程序外部的任何浏览器访问时,移动网络和Android检测到该设备,但它表示当我调用android本机应用程序时,函数没有定义:

   <script>Android.showToast("Welcome");</script>

So the question is, how can i detect if i'm using a web browser or a native app? 所以问题是,如何检测我是使用网络浏览器还是原生应用程序? Or otherwise, how can i avoid the web mobile getting down for that error? 或者,我怎样才能避免网络手机因此错误而陷入困境?

Grettings!!And thx for your help! Grettings !!并为您提供帮助!

via PHP you can check the user-agent that is trying to access your web page. 通过PHP您可以检查尝试访问您的网页的user-agent you can test with one of these. 你可以测试其中一个。

if( $_SERVER['HTTP_X_REQUESTED_WITH'] == 'your_android_app_package_name' ) {
   //...Inject your javascript code

} }

if( strpos($_SERVER['HTTP_USER_AGENT'], 'your_android_app_package_name/') !== false ){
   //...Inject your  javascript code.

} }

This happened to me a lot when I was using components that had environment dependencies in javascript. 当我在javascript中使用具有环境依赖性的组件时,这发生在我身上。 The way I usually do it is overwrite the global object with a local object and empty functions. 我通常这样做的方式是用本地对象和空函数覆盖全局对象。 In your case, you could try adding something like this before all the other script tags: 在您的情况下,您可以尝试在所有其他脚本标记之前添加这样的内容:

window.Android = window.Android || {
    "showToast": function(){}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM