scheme host 不在 android lollipop 上工作,点击 link 打开应用程序

scheme host not working on android lollipop, click on link to open app

我正在使用这段代码从 link 启动我的应用程序。

<activity
        android:name="com.example.myApp.myClass"
        android:label="@string/app_name" >
    <intent-filter>
        <data
            android:host="customHostName"
            android:scheme="customScheme" />
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.BROWSABLE" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>  

This is href link,我想拿到最后的钥匙。

customScheme://customHost/49FYTJTF00

它在 android 的所有先前版本上运行良好,但在 Lollipop 上运行不正常。
当我单击 link 时,它只显示要启动的浏览器列表。

我该怎么办?

请使用路径前缀。

   android:pathPrefix="/"

它可能会解决您的问题。 请关注android developer guideURL.

编辑:

经过测试和测试,我发现如果您的方案包含大写字符,浏览器将无法启动它。您的方案应仅包含小写字符!

另请注意,chromium 项目的错误 459156 仍然不允许您直接启动 url,您应该向用户推荐包含此 JavaScript 代码的网页:

<!DOCTYPE html>
<html>
    <body>
        <script language="javascript">
            window.location = 'customscheme://customHost/49FYTJTF00';
        </script>
    </body>
</html>

登陆此页面的用户将自动收到 Activity 选择器对话框提示或直接发送到您的 Activity。

要尝试一下,请打开 Android 浏览器转到下面的 url 并将上面的代码片段复制粘贴到编辑器中: http://www.w3schools.com/html/tryit.asp?filename=tryhtml_intro

显示浏览器的 Gif + JavaScript 打开 Activity

原版post

我试用了您的自定义 URI,它适用于 Android 5.0

但是你应该知道以下两个bugs/issues:

  1. Bug 459156 of the Chromium project This basicly means launching custom schemes from the Android browser does not work unless the URL is applied using JavaScript. For a workaround see this Whosebug post: OAuth and custom scheme result in a "ERR_UNKNOWN_URL_SCHEME" in Chrome
  2. Bug 80971: URI with custom scheme are not clickable in SMS Text (Linkify?)

我的做法

我创建了两个单独的活动,一个作为意图接收器,另一个作为意图发射器。启动 activity 有一个可以输入完整 URI 的 EditText 和一个用于启动输入的 URI 的按钮。

Main.java onClick(启动 activity)

@Override
public void onClick(View view) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(Uri.parse(input.getText().toString()));
    try {
        startActivity(intent);
    } catch(ActivityNotFoundException e){
        Toast.makeText(this, "Auww!! No Activity can open this URI!", Toast.LENGTH_SHORT).show();
    }
}

manifest.xml(仅活动)

注意 <data android:pathPattern=".*"/> 部分。这部分很重要,因此主机之后的任何内容都将被接受为有效。

<activity
    android:name=".Main"
    android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
    </intent-filter>
</activity>

<activity
    android:name=".UriActivity"
    android:label="@string/app_name">

    <!--
    This intent-filter will open any URI with the following forms:
    
    customscheme://customHost/49FYTJTF00
    customscheme://customHost
    https://www.google.com/something/something
    http://www.google.com/
    http://google.com/something
    -->

    <intent-filter>
        <data android:scheme="https"/>
        <data android:scheme="http"/>
        <data android:host="google.com"/>
        <data android:host="www.google.com"/>

        <data android:scheme="customscheme"/>
        <data android:host="customHost"/>

        <data android:pathPattern=".*"/>

        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
    </intent-filter>
</activity>

UriActivity.java(接收activity)

public class UriActivity extends ActionBarActivity {

    private TextView tvText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_uri);
        tvText = (TextView) findViewById(R.id.text);
        setTextFromIntent();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setTextFromIntent();
    }

    private void setTextFromIntent(){
        StringBuilder text = new StringBuilder();

        Uri data = getIntent().getData();
        if(data != null){
            text.append("Path:\n");
            text.append(data.getPath());

            text.append("\n\nScheme:\n");
            text.append(data.getScheme());

            text.append("\n\nHost:\n");
            text.append(data.getHost());

            text.append("\n\nPath segments:\n");
            text.append(Arrays.toString(data.getPathSegments().toArray()));
        } else {
            text.append("Uri is null");
        }
        tvText.setText(text);
    }
}

截图:

测试工程下载:

如果你不想自己尝试,我为这个项目做了一个download

您写道:

    <data
        android:host="customHostName"
        android:scheme="customScheme" />

所以,使用customScheme://customHostName/49FYTJTF00
而不是 customScheme://customHost/49FYTJTF00

而不是使用 link 到 customScheme://customHost/49FYTJTF00,你有没有试过使用 link like

<a href="intent://customHostName/49FYTJTF00#Intent;scheme=customScheme;end">

Chrome 应该可以在您的应用中正常打开它。同时,这可能不适用于所有浏览器。

请记住,如果您想要 运行 来自 Google Chrome 的应用程序,您应该在 another way 中执行此操作。