从浏览器启动Android应用程序

Tim*_*Tim 5 android android-intent

我查看了几个与我相似的Stack Overflow问题.每一个都得到回答,原作者似乎很满意,但当我试图复制他们的结果时,我空手而归.显然我正在做一些可怕的错误,但我似乎无法弄明白.

作为参考,这些是我一直在关注的问题:

最终,我希望在用户执行OAuth身份验证请求后使用此功能重新启动我的应用程序.但是,我的实际应用程序有点太大了,无法在此发布.对于这个问题,我已经整理了一个简单的应用程序,向您展示我已经完成的工作.我有两个活动.这主要是因为我见过的每个例子都使用VIEW动作来进行活动.我更喜欢使用MAIN操作,但我在这里使用两者来表明我尝试了两种方法.

AndroidManifest.xml(原文;见下面的编辑版)

<?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>
Run Code Online (Sandbox Code Playgroud)

AndroidManifest.xml(编辑以回复评论)

<?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>
Run Code Online (Sandbox Code Playgroud)

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"
>
Run Code Online (Sandbox Code Playgroud)

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!");
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

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!");
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

这足够愉快.然后,在浏览器中,如果我输入ex1:// helloworld或ex2:// helloworld,我会得到不同的结果,具体取决于它是如何完成的.如果我在网址栏中输入它,我会在谷歌搜索ex1:// helloworld.如果我通过重定向来实现,我会得到"网页不可用".如果我尝试直接使用类似于以下内容的方式启动意图:

    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);
Run Code Online (Sandbox Code Playgroud)

我得到默认的"Hello my-example!".我不确定我做错了什么.任何见解?

Com*_*are 11

您无法使用MAIN- Android中的网址仅VIEW来自浏览器或WebView.

此外,您的所有Intent过滤器都不包含该BROWSABLE类别,因此它们永远不会与浏览器一起使用.

不建议您发明自己的方案.对于您控制的某个域,您可以使用常规HTTP方案.此示例项目演示了这一点.特别是,您可以按照此过滤器显示的模式:

<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>
Run Code Online (Sandbox Code Playgroud)

替换www.this-so-does-not-exist.com你拥有的东西,并可能添加一个path来缩小过滤器的范围.您还可以看到ZXing的条形码扫描仪应用程序使用的这种技术.