从浏览器访问Android API [英] accessing Android API from browser

查看:104
本文介绍了从浏览器访问Android API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,如果我使用嵌入式WebView,则可以从javascript调用android native API.

I understand I can call android native APIs from javascript if I use an embedded WebView.

但是,是否可以从Android的常规浏览器(例如Firefox Mobile或常规浏览器)访问这些API?我认为我必须在某处显式授予许可,否则这将是一个很大的安全漏洞.

However, is it possible to access these APIs from a regular browser in Android, such as Firefox Mobile or the stock browser? I assume I would have to explicitly grant permission somewhere, as otherwise this would be a big security hole.

我无法看到创建运行javascript的特殊应用程序的用法;我的应用程序是运行在服务器上的常规网络应用程序(未嵌入我的android应用程序中),并且经常更新(因此不适合嵌入到应用程序中),在某些情况下,增加直接查询各种信息的功能将非常有用从网页上删除,主要是为了防止Android杀死浏览器并在重新输入时强制长时间重新加载该Web应用程序,因为我不得不花5秒钟才能跳出(例如)Firefox才能手动获取信息.

I'm having trouble seeing the use of creating a special app that runs javascript; my app is a regular web app running off a server (not embedded in my android app) and is updated frequently (therefore not suitable for embedding in an app) and in some cases it would be useful to add the ability to query various information directly from the web page, mainly to prevent Android from killing the browser and forcing a lengthy reload of the web app upon re-entry just because I had to take 5 seconds to jump out of (e.g.) Firefox to get the info manually.

推荐答案

我不认为您可以通过常规浏览器调用Android API(它们没有作为DOM模型的一部分),除非您可以为每个浏览器构建自己的特定扩展名.那将是乏味的工作,可能不值得,因为这正是存在嵌入式WebView(例如Phonegap)的原因.

I don't think there is anyway you can call your Android APIs from a regular browser (they do not have them as part of the DOM model), unless you build specific extensions of your own for each browser. That would be tedious work and probably not worth it, since that is exactly why Embedded WebViews exist (like Phonegap).

现在,您始终可以尝试其他方法,即直接从服务器上的WebView加载网页.默认情况下,Web视图从设备的本地存储中加载其内容,但是,使用XHR,您可以调用传统的网页并将其显示在Web视图中.

Now, you can always try do the other way around, which is load your web page from a WebView right from your server. By default, webviews load their content from the local storage of the device, however, using XHR you can call your traditional web pages and show them in the webview.

这可以通过使用div来实现,该div可以嵌入服务器中的内容,也可以使用iframe从浏览器中完全加载整个页面.第二种方法将更加透明,因为您可以完全重用当前的网页,但是由于跨源问题的安全性限制,它会阻止您将包装程序与网页进行通信.我建议采用第一种方法,尽管您可能必须重构服务器网页,以使其与您的应用程序更好地集成.

This can be achieved using either divs that can embed a piece of content from your server or iframes to fully load entire pages from your browser. The second approach will be more transparent since you can fully reuse your current web pages, however it will prevent you from communicating your wrapper with the web page, due to cross origin issues security constraints. I would suggest the first approach, although you probably will have to refactor your server web pages to make them integrate better with your application.

这将像这样工作(为简单起见,我在示例中使用的是JQuery,但您可以直接使用XHR进行操作)

This would work like this (for simplicity in the examples I am using JQuery, but you could do it directly using XHR):

  1. 您可以在Web视图中创建一个微小的index.html(phonegap或您正在使用的任何东西),然后定义一个充当包装程序来调用服务器的

  1. You create a tiny index.html in your webview (phonegap or whatever you are using) and define a that will act as a wrapper to do the calling to your server:

<html>
<head>
<!-- Load all your css and javascript stuff here -->
</head>
<body>
<div id="wrapperdiv"></div>
</body>
</html>

<html>
<head>
<!-- Load all your css and javascript stuff here -->
</head>
<body>
<div id="wrapperdiv"></div>
</body>
</html>

包括一个可从服务器动态加载内容的JavaScript:

Include a javascript to dynamically load the content from your server:

$(function() {
    $.ajax({
        url: 'http://www.michaels-server.com/your-web-endpoint',
        type: 'GET',
        crossDomain: true,
        success: function(data, textStatus, xhr) {
            $('#wrapperdiv').html(data);
        }
        error: function() { alert("Cannot contact server."); }
    });
});

  • 配置嵌入式Web视图以允许跨源请求.在phonegap中,您可以执行以下操作:

  • Configure your embedded webview to allow cross origin requests. In phonegap you do something like:

    < widget ...>

    < ;!-此处的窗口小部件配置内容->

    < access origin ="*" />

    <!-更多的小部件配置资料->

    </widget>

    <widget ...>

    <!-- widget configuration stuff here -->

    <access origin="*" />

    <!-- more widget configuration stuff -->

    </widget>

    您的html包装器中的div现在将动态加载服务器中的内容.该页面中的任何JavaScript都可以使用嵌入式Webviews DOM模型(例如Phonegap提供的模型)来调用Native API.

    The div in your html wrapper will now dynamically load the content from your server. Any javascript in that page can call the Native APIs using the embedded webviews DOM model, such as the one provided by Phonegap.

    希望这会有所帮助.

    这篇关于从浏览器访问Android API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆