有没有办法从一个Android浏览器的应用程序传递消息? [英] Is there a way to pass a message from an Android browser to an app?

查看:96
本文介绍了有没有办法从一个Android浏览器的应用程序传递消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,我想对于一些数据从一个手机网站传递给一个原生的Andr​​oid应用程序。复杂性在于,在正常情况下,原生Android应用程序可能无法在手机网站加载时安装。

下面是一个例子:

我ExampleApp中的一个用户,我想分享一个特定的数据从ExampleApp中有你,谁也从来没有安装ExampleApp中。

我送你一封电子邮件,链接到它知道你想,当你打开链接,看看有什么数据块移动网页。但是,由于数据只能在本机应用程序,而不是移动网络,你带到一个页面,要求你去到Android Market和安装ExampleApp中。

您安装ExampleApp中,理想情况下,你会直接带到一块数据,我与您共享。

最大的问题是在观看移动网页和ExampleApp中安装之间的脱节。

我想过了几个解决方案,但也获得了成功(这么说,我可能只是被错误地执行这些):

  1. 设置,包括数据时的移动网页加载在我们的域的cookie。然后,当ExampleApp中被打开,开始的WebView请求页面上相同的域,并检查cookie的值。用它来确定哪些数据块在ExampleApp中显示。
  2. 使用的JavaScript的localStorage存储一个参考的数据块并使用的WebView来获得在该领域的网页,并从内部ExampleApp中要求的localStorage的内容。

在这两种情况下,却仿佛web视图和浏览器都sandbox'd远离对方,所以你不能在两者之间的饼干/ localStorage的。

是否有任何其他的方式来留下一个带crumbtrail或将在以后安装一个应用程序可以通过浏览器访问的消息?

编辑:鉴于一些答复,我要指出,我只希望你点击一次链接,不必单击一次,安装应用程序,然后再次单击打开应用程序,以正确的地方

解决方案

解决的办法其实很简单,我有点惊讶,没有人能够拿出它了一年多。所以在这里,它是:

的基本思想是使用Cookie来存储信息。该数据流如下:

打开网页 - >设置cookie的 - >重定向到市场 - >安装的应用程序 - >启动浏览器,网页 - >阅读饼干 - >开始定制意图与cookie数据 - >拍摄意图和读取cookie数据。

的用户访问的网页(通过扫描的QR标记例如)。该网页的网址持有引用要传递的数据,或者保存数据本身。该网站设置数据作为cookie并重定向到Android市场(http://market.android.com/details?id=com.yourapp)。只要你的应用程序启动后,将打开浏览器,然后重新装入相同的网页。然而,这一次,它检测到的Cookie已设置并重定向到类似的例子自定义URL方案:?//example.com/installed ID而不是市场=数据。使用一个意图过滤器,启动你的活动,并从中提取意图的信息。

下面是相关活动code:

 公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    意向意图= getIntent();
    如果(意向== NULL || intent.getData()== NULL ||!榜样.equals(intent.getData()。getScheme())){
        字符串URL =htt​​p://example.com/yourApp/index.php;
        意图I =新的意图(Intent.ACTION_VIEW);
        i.setData(Uri.parse(URL));
        startActivity(ⅰ);
        完();
        返回;
    }其他 {
        开放的我们的uri = intent.getData();
        如果(榜样.equals(uri.getScheme())){
            ID = uri.getQueryParameter(ID);
        }
    }
    ...初始化你的活动...
}
 

一个简单的PHP文件(网页),便于演示:

 < PHP
$值=123;
如果(!使用isset($ _ COOKIE [TestCookie])){
    setCookie方法(TestCookie,$值,时间()+ 3600); / *到期在1个小时* ​​/
    标题(位置:http://market.android.com/details?id=com.yourapp);
    出口;
}其他 {
    头(?地点:例如://example.com/installed ID =$ _ COOKIE [TestCookie]);
    出口;
}
?>
 

和意图过滤器:

 <  - 就像忠诚手柄网址://example.com/merchant/123  - >
<意向滤光器>
    <作用机器人:名称=android.intent.action.VIEW/>
    <类机器人:名称=android.intent.category.DEFAULT/>
    <类机器人:名称=android.intent.category.BROWSABLE/>
    <数据机器人:计划=榜样机器人:主机=example.com/>
&所述; /意图滤光器>
 

顺便说一句,我已经决定博客了解的问题。

I have a situation where I'd like for some data to be passed from a mobile web site to a native Android app. A complication is that, in the normal case, the native Android app might not be installed when the mobile web site is loaded.

Here's an example:

I am a user of ExampleApp and I want to share a particular piece of data from ExampleApp with you, who has never installed ExampleApp.

I send you an email with a link to a mobile web page that knows what piece of data you want to see when you open the link. But, since the data is only available in the native app, not the mobile web, you're taken to a page that asks you to go to Android Market and install ExampleApp.

You install ExampleApp, and ideally, you'd be taken directly to the piece of data that I shared with you.

The big problem is in the disconnect between viewing the mobile web page and the installation of ExampleApp.

I've thought about a couple solutions, but neither have been successful (that said, I could just be implementing them incorrectly):

  1. Set a cookie on our domain that includes the data when the mobile web page is loaded. Then, when ExampleApp is opened, start a WebView to request a page on the same domain and check the value of the cookie. Use that to determine what piece of data to show in ExampleApp.
  2. Use JavaScript localStorage to store a reference to the piece of data and use a WebView to get a page on that domain and request the content of localStorage from within ExampleApp.

In both these situations, it seems as though the WebView and the Browser are sandbox'd away from each other, so you can't get at the cookies/localStorage between the two.

Is there any other way to "leave a crumbtrail" or set a message that an app installed later can access from the Browser?

EDIT: Given some of the responses, I should mention that I only want you to click once on the link, NOT have to click once, install the app, then click again to open the app to the right place.

解决方案

The solution is actually quite simple, I'm a bit astonished that nobody has been able to come up with it for over a year. So here it is:

The basic idea is to use cookies to store the information. The data flow is as follows:

open web page -> set cookie -> redirect to market -> install app -> launch browser with web page -> read cookie -> start custom intent with cookie data -> capture intent and read cookie data

The user visits a webpage (by scanning a QR Tag for example). The url of the webpage holds a reference to the data you want to pass, or holds the data itself. The website sets the data as a cookie and redirects to the Android market (http://market.android.com/details?id=com.yourapp). As soon as your app launches, you open a browser and load the same webpage again. This time however, it detects that the cookie is already set and redirects to a custom URL scheme like example://example.com/installed?id=DATA, instead of the market. Using an intent filter, you launch your activity and extract the information from the intent.

Here is the relevant Activity code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    if(intent == null || intent.getData() == null || !"example".equals(intent.getData().getScheme())) {
        String url = "http://example.com/yourApp/index.php";
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse(url));
        startActivity(i);
        finish();
        return;
    }else {
        Uri uri = intent.getData();
        if("example".equals(uri.getScheme())) {
            id = uri.getQueryParameter("id");
        }
    }
    ... initialise your activity ...
}

A simple PHP file (the website) for easy demonstration:

<?php
$value = "123";
if(!isset($_COOKIE["TestCookie"])) {
    setcookie("TestCookie", $value, time()+3600);  /* expire in 1 hour */
    header("Location: http://market.android.com/details?id=com.yourapp");
    exit;
}else {
    header("Location: example://example.com/installed?id=".$_COOKIE["TestCookie"]);
    exit;
}
?>

And the intent filter:

<!-- Handle URLs like loyalty://example.com/merchant/123 -->
<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme="example" android:host="example.com" />
</intent-filter>

BTW, I have decided to blog about the issue.

这篇关于有没有办法从一个Android浏览器的应用程序传递消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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