【问题标题】:scheme host not working on android lollipop, click on link to open app方案主机无法在 android 棒棒糖上运行,单击链接以打开应用程序
【发布时间】:2015-05-20 21:55:35
【问题描述】:

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

<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>  

这是href链接,我想最后拿到钥匙。

customScheme://customHost/49FYTJTF00

它在所有以前版本的 android 上都可以正常工作,但不能在 Lollipop 上工作。
当我单击该链接时,它仅显示要启动的浏览器列表。

我该怎么办?

【问题讨论】:

  • 你能分享你的网址吗?包括您的前缀路径.. 也尝试更改 sdk 21

标签: android android-intent hyperlink android-5.0-lollipop intentfilter


【解决方案1】:

编辑:

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

另外请注意,铬项目的错误 459156 仍然不允许您直接启动 url,您应该将用户引用到包含此 JavaScript 代码的网页:

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

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

要试试看,打开安卓浏览器去下面的url,把上面的sn-p复制粘贴到编辑器里: http://www.w3schools.com/html/tryit.asp?filename=tryhtml_intro

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

原帖

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

但您应该注意以下两个错误/问题:

  1. Bug 459156 of the Chromium project 这基本上意味着从 Android 浏览器启动自定义方案不起作用,除非使用 JavaScript 应用 URL。有关解决方法,请参阅此 StackOverflow 帖子: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?)

我的方法

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

Main.java onClick(启动活动)

@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();
    }
}

ma​​nifest.xml(仅限活动)

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

<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(接收活动)

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

【讨论】:

  • 感谢@Rolf,但当方案为 http 或 https 但我使用自定义方案时它可以工作......但如果我使用 http/https 作为方案并单击电子邮件应用程序中的链接,它显示打开对话框或打开选择启动 URL 即浏览器的默认应用程序。我想让它打开我的应用程序。
  • @Salmaan 自定义 URI 对我有用(见屏幕截图)。您是否阅读过有关 chromium 浏览器的错误报告?除非使用 window.location (JavaScript) 加载,否则不支持自定义方案
  • 您可以下载项目并删除 google.com 和 http/ https 部分并自己尝试;)
  • @Salmaan 我刚刚尝试使用 JavaScript 从 Android 5.0 浏览器启动自定义方案,但我发现 ti 不起作用,但是当我使用小写方案时一切正常!所以你应该使用小写方案。
  • 别忘了 JavaScript 部分;)我还更新了可下载的源代码!
【解决方案2】:

请使用路径前缀。

   android:pathPrefix="/"

它可能会解决您的问题。 请关注android developer guide网址。

【讨论】:

    【解决方案3】:

    您是否尝试过使用指向 customScheme://customHost/49FYTJTF00 的链接,而不是使用类似的链接

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

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

      • 这不仅与 chrome 有关,还与用户拥有的任何电子邮件应用程序或他使用的任何浏览器有关
      • @Salmaan 我不会为您提供所有情况的解决方案,但我很确定我提到的方法会对您使用 Chrome 有所帮助。
      【解决方案5】:

      你写的:

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

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

      【讨论】:

      • 这只是问题中的一个错字(例如),OP说它以前曾工作过。
      猜你喜欢
      • 2015-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多