在Android浏览器/ WebView中处理链接以直接启动应用程序

3
我希望我的应用程序能够通过链接打开,按照以下在Android浏览器中创建链接以启动我的应用程序? 的说明进行操作。嗯,它的效果很好(大多数情况下...这就是我寻求帮助的原因)。
我的html链接如下:
<a href="intent:#Intent;action=com.mark.MY_ACTION;end">Open Application</a>

我的意图过滤器形式如下

<intent-filter>
     <action android:name="com.mark.MY_ACTION"/>
     <category android:name="android.intent.category.DEFAULT">
     <category android:name="android.intent.category.BROWSABLE">
</intent-filter>

目前为止还不错(希望如此)。它在大多数浏览器中都能正常工作。

为了测试以上内容,我编写了一个演示活动。

final WebView webview = new WebView(this); 
setContentView(webview); 
webview.loadUrl("http://www.myhomepage.com/"); 

网页http://www.myhomepage.com/有一些自定义链接/ href(包括 inent 链接)。

点击常规的链接会在浏览器中打开这些链接,但是点击我的“特殊链接”(即 intent 链接)会打开一个 404 页面:

网页不可用
intent:#Intent;action=com.mark.MY_ACTION;end
可能暂时无法访问...

戴安娜在上面的链接中提到:

它们允许您将启动仅定向到您的应用程序,而用户无需选择转而使用浏览器或任何其他应用程序。


我相信这就是你要找的内容: https://dev59.com/7HA85IYBdhLWcg3wD_O8#2958870 - Mars
1
这是使用Scheme的一种方法,但正如我的导师Diane所说:“我们强烈反对人们使用自己的方案,除非他们正在定义一个新的全球互联网方案。” 我想使用意图:... - Omer
3个回答

1

你的Intent Filter与Diana提到的有些相似

<activity android:name=".AntonWorld"
      android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <intent-filter>
        <data android:scheme="anton" />
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.BROWSABLE" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

所以你的应该像这样:

<intent-filter> 
    <data android:scheme="com.mark.MY_ACTION"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <category android:name="android.intent.category.BROWSABLE"/>
</intent-filter>

未测试是否需要 android.intent.category.VIEW。如果有效,请告诉我。


Diana也说她在子元素的顺序上遇到了一些问题。也许你需要在android.intent.category.Default之前加上android.intent.category.BROWSEABLE。 - AliDeo
我不想让我的URL变成anton://www.....com,我想将一个隐式意图转发到我的应用程序。 - Omer
现在无法识别,因为您更改了以下Intent Filter子元素: <data android:scheme=""> - AliDeo
你的链接是code<a href="intent:#Intent;action=com.mark.MY_ACTION;end">打开应用程序</a>我认为应该是<a href="com.mark.MY_ACTION://some_info">打开应用程序</a>,我会创建一个测试类并进行验证。 - AliDeo
你所建议的是另一种方法。它与“intent:”方法基本相同。 请尝试在此浏览器中使用您的方法 http://code.google.com/p/zirco-browser/ - Omer

1

试试我的简单技巧:

        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if(url.startsWith("classRegister:")) {                  
                Intent MnRegister = new Intent(getApplicationContext(), register.class); startActivity(MnRegister);
            }               
            view.loadUrl(url);
            return true;
        }

还有我的HTML链接:

<a href="classRegister:true">Go to register.java</a>

或者你可以让 < a href="classRegister:true" > 的 class 文件名的值为"true"

不过这个脚本只适用于mailto链接 :)

        if (url.startsWith("mailto:")) {
            String[] blah_email = url.split(":");
            Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
            emailIntent.setType("text/plain");
            emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{blah_email[1]});
            emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, what_ever_you_want_the_subject_to_be)");
            Log.v("NOTICE", "Sending Email to: " + blah_email[1] + " with subject: " + what_ever_you_want_the_subject_to_be);
            startActivity(emailIntent);
        }

1
在Manifest.xml中添加以下代码:
<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.parag-chauhan.com"
        android:path="/test"
        android:scheme="http" />
</intent-filter>

为测试创建另一个项目并运行以下代码

Intent i = new Intent(
    Intent.ACTION_VIEW , Uri.parse("http://www.parag-chauhan.com/test")
);
startActivity(i);

你可以在链接中传递参数。更多信息请参阅我的博客文章如何在Android浏览器中创建一个链接以启动我的应用程序?

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接