简体   繁体   English

从Android中的链接问题启动应用

[英]Launch an app from a link issue in Android

Suppose I have an app with the following intent filter in an activity in the AndroidManifest.xml file: 假设我在AndroidManifest.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="blabla.com"
                android:pathPrefix="/element/"
                android:scheme="http" />
        </intent-filter>

Of course it will cause my app to be launched from the browser, for example. 当然,这将导致例如从浏览器启动我的应用程序。 However, when the user clicks on the link, it is asked to select the application for opening that link. 但是,当用户单击链接时,会要求您选择用于打开该链接的应用程序。 If he/she selects a browser instead of my app and, in addition, he/she checks the "don't ask me again" check box, my app will never be launched! 如果他/她选择了浏览器而不是我的应用程序,并且他/她还选中了“不再询问我”复选框,则我的应用程序将永远不会启动!

  1. Is there any way to avoid that? 有什么办法可以避免这种情况?
  2. Is there another kind of URI I can use to uniquely open my app? 我可以使用另一种URI来唯一打开我的应用程序吗? I have been looking for answers in StackOverflow but I have not found any good example of a non browsable URI for launching my app from a link. 我一直在StackOverflow中寻找答案,但找不到用于从链接启动我的应用程序的不可浏览URI的任何示例。

Thank you so much, 非常感谢,

Use an with a element. 与元素一起使用。 For example, to handle all links to twitter.com, you'd put this inside your in your AndroidManifest.xml: 例如,要处理所有指向twitter.com的链接,请将其放在您的AndroidManifest.xml中:

<intent-filter>
<data android:scheme="http" android:host="twitter.com"/>
<action android:name="android.intent.action.VIEW" />
</intent-filter>

Then, when the user clicks on a link to twitter in the browser, they will be asked what application to use in order to complete the action: the browser or your application. 然后,当用户在浏览器中单击指向twitter的链接时,系统将询问他们使用哪个应用程序以完成操作:浏览器还是您的应用程序。

Of course, if you want to provide tight integration between your website and your app, you can define your own scheme: 当然,如果您想在网站和应用之间提供紧密的集成,则可以定义自己的方案:

<intent-filter>
<data android:scheme="my.special.scheme" />
<action android:name="android.intent.action.VIEW" />
 </intent-filter>

Then, in your web app you can put links like: 然后,在您的网络应用中,您可以放置​​如下链接:

<a href="my.special.scheme://other/parameters/here">
And when the user clicks it, your app will be launched automatically (because it will probably be the only one that can handle my.special.scheme:// type of uris). The only downside to this is that if the user doesn't have the app installed, they'll get a nasty error. And I'm not sure there's any way to check.
  1. Use a custom scheme instead of http. 使用自定义方案而不是http。 Maybe, x-myapp 也许,x-myapp

      <data android:host="blabla.com" android:pathPrefix="/element/" android:scheme="x-myapp" /> 
  2. Have your blabla.com web server respond to http links ( http://blabla.com/element/foo ) with a redirect to x-myapp://blabla.com/element/foo for mobile android devices.....or put a link on the page that will have the x-myapp:// link for the user to tap 让您的blabla.com网络服务器响应http链接( http://blabla.com/element/foo ),并重定向到移动Android设备的x-myapp://blabla.com/element/foo。或在页面上放置一个链接,该链接将带有x-myapp://链接供用户点击

Now since your app is the only one responding to x-myapp://blabla.com/element/ links, you have avoided step 1. 现在,由于您的应用程序是唯一响应x-myapp://blabla.com/element/链接的应用程序,因此您无需执行步骤1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM