从浏览器启动一个Android应用 [英] Launching an Android Application from the Browser

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

问题描述

我已经看了几个堆栈溢出问题,类似的标题我的。每回答,并原作者似乎满意,但是当我试图复制他们的结果我拿出空手而归。很显然,我做一些可怕的错误,但我似乎无法找到答案。

I have looked at several Stack Overflow questions with similar titles to mine. Each is answered, and the original author seems satisfied, but when I try to replicate their results I come up empty handed. Obviously I'm doing something horribly wrong, but I can't seem to figure it out.

有关的参考,这些都是我一直在寻找的问题:

For reference, these are the questions that I've been looking at:

  • <一个href="http://stackoverflow.com/questions/2958701/launch-custom-android-application-from-android-browser">Launch定制的Andr​​oid应用程序从Android浏览器
  • <一个href="http://stackoverflow.com/questions/3469908/make-a-link-in-the-android-browser-start-up-my-app">Make在Android浏览器中的链接启动我的应用程序
  • Android开发回调URL不工作
  • <一个href="http://stackoverflow.com/questions/3586627/android-intent-filters-how-do-you-use-intent-category-browsable-correctly">Android意图过滤器:你怎么使用意向类别可浏览正确
  • Launch Custom Android Application From Android Browser
  • Make a Link in the Android Browser Start Up My App
  • Android Dev Callback Url Not Working
  • Android Intent Filters: How Do You Use Intent Category Browsable Correctly

最后,我想用这个一旦用户执行了OAuth的认证请求重新启动我的应用程序。但是,我真正的应用程序是一个有点太大,张贴在这里。对于这个问题,我已经把一个简单的应用程序,向你展示我做了什么。我有两个活动。这主要是因为每一个我所见过的例子使用视图操作使用的活动。我倒是preFER使用的主要行动,但我用两个这里要说明,我都尝试。

Ultimately, I want to use this to restart my application once the user has performed an OAuth authentication request. However, my real application is a bit too large to post here. For this question, I've put together a simple application, to show you what I've done. I have two activities. This is mostly because every example I've seen uses an activity using the VIEW action. I'd prefer to use the MAIN action, but I've used both here to show that I tried both.

AndroidManifest.xml中(原件;见下面编辑的版本)

AndroidManifest.xml (Original; see edited version below)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="org.example" android:versionCode="1" android:versionName="1.0-SNAPSHOT">
<application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
    <activity android:name=".HelloAndroidActivity">
        <intent-filter>
            <data android:scheme="ex1"/>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    <activity android:name=".CallbackActivity">
        <intent-filter>
            <data android:scheme="ex2"/>
            <action android:name="android.intent.action.VIEW"/>
        </intent-filter>
    </activity>
</application>

AndroidManifest.xml中(编辑回应评论)

AndroidManifest.xml (Edited in response to comment)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="org.example" android:versionCode="1" android:versionName="1.0-SNAPSHOT">
<application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
    <activity android:name=".HelloAndroidActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    <activity android:name=".CallbackActivity">
        <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:host="www.this-so-does-not-exist.com" android:scheme="http" />
        </intent-filter>
    </activity>
</application>

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

HelloAndroidActivity.java

HelloAndroidActivity.java

package org.example;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;

public class HelloAndroidActivity extends Activity {

    private TextView textView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textView = (TextView) findViewById(R.id.textView);
    }

    @Override
    public void onResume() {
        super.onResume();
        Intent intent = getIntent();
        Uri data = intent.getData();
        if (data != null) {
            textView.setText("Hello Web!");
        }
    }

}

CallbackActivity.java

CallbackActivity.java

package org.example;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;

public class CallbackActivity extends Activity {

    private TextView textView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textView = (TextView) findViewById(R.id.textView);
    }

    @Override
    public void onResume() {
        super.onResume();
        Intent intent = getIntent();
        Uri data = intent.getData();
        if (data != null) {
            textView.setText("Hello Web!");
        }
    }

}

此建立愉快就好了。然后,在浏览器中,如果我输入EX1://的HelloWorld或EX2://的HelloWorld,我得到不同的结果,这取决于它是如何做。如果我在地址栏输入它,我收到了谷歌搜索EX1://的HelloWorld。如果我通过重定向做到这一点,我得到找不到网页。如果我尝试直接用类似的东西开始的意图:

This builds happily enough. Then, inside the browser, if I enter ex1://helloworld or ex2://helloworld I get different results depending upon how it is done. If I enter it in the url bar I get a google search for ex1://helloworld. If I do it via a redirect, I get "Web page not available". If I try to start the intent directly with something similar to:

    Intent intent = new Intent(Intent.ACTION_MAIN);
    ComponentName componentName = new ComponentName(
            "org.example",
            "org.example.HelloAndroidActivity");
    intent.setComponent(componentName);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setData(Uri.parse("ex1://helloworld"));
    intent.addCategory(Intent.CATEGORY_BROWSABLE);
    startActivity(intent);

我得到默认的你好,我-榜样!。我不知道我在做什么错误。任何见解?

I get the default "Hello my-example!". I'm not sure what I'm doing incorrectly. Any insights?

推荐答案

您无法使用 - 在Android的网址只查看从浏览器中编辑或的WebView

You cannot use MAIN -- URLs in Android are only VIEWed from the browser or WebView.

此外,没有你的意图过滤器包括可浏览类,所以他们永远不会与浏览器的工作。

Also, none of your Intent filters include the BROWSABLE category, so they will never work with the browser.

不建议去创造你自己的计划。您可以使用常规的HTTP方案,为您控制一些领域。 演示了此示例项目。特别是,你可以按照这个过滤器显示模式:

It is not recommended to invent your own schemes. You can use a regular HTTP scheme, for some domain that you control. This sample project demonstrates this. In particular, you can follow the pattern shown by this filter:

<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:host="www.this-so-does-not-exist.com" android:scheme="http" />
</intent-filter>

替换 www.this-so-does-not-exist.com 与你自己的东西,也许在路径添加缩小过滤器的范围。你也可以看到这种技术使用的吧code扫描仪从 ZXing 的应用程序。

replacing www.this-so-does-not-exist.com with something you own, and perhaps adding in a path to narrow the filter's scope. You can also see this technique used by the Barcode Scanner app from ZXing.

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

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