【发布时间】:2023-03-03 00:46:01
【问题描述】:
谁能指导我如何从安卓浏览器启动我的安卓应用程序?
【问题讨论】:
标签: android android-intent intentfilter
谁能指导我如何从安卓浏览器启动我的安卓应用程序?
【问题讨论】:
标签: android android-intent intentfilter
例如,你有接下来的事情:
打开您的应用的链接:http://example.com
你的应用的包名:com.example.mypackage
那你需要做下一步:
为您的活动添加一个意图过滤器 (可以是您想要的任何活动。有关更多信息,请查看documentation)。
<activity
android:name=".MainActivity">
<intent-filter android:label="@string/filter_title_view_app_from_web">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- Accepts URIs that begin with "http://example.com" -->
<data
android:host="example.com"
android:scheme="http" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
创建一个 HTML 文件来测试链接或使用this methods.
直接打开您的活动(只需打开您的活动,无需选择对话框)。
Open this link with browser or your programm (by choosing dialog).
使用移动版 Chrome 进行测试
就是这样。
而且没有必要在市场上发布应用来测试深度链接 =)
另外,有关更多信息,请查看documentation 和有用的presentation。
【讨论】:
Xamarin 移植 Felix 的答案
在您的 MainActivity 中,添加以下内容(文档:Android.App.IntentFilterAttribute Class):
....
[IntentFilter(new[] {
Intent.ActionView },
Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
DataScheme = "my.special.scheme")
]
public class MainActivity : Activity
{
....
Xamarin 将在 AndroidManifest.xml 中为您添加以下内容:
<activity
android:label="Something"
android:screenOrientation="portrait"
android:theme="@style/MyTheme"
android:name="blah.MainActivity">
<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:scheme="my.special.scheme" />
</intent-filter>
</activity>
为了获取参数(我在 MainActivity 的 OnCreate 中测试过):
var data = Intent.Data;
if (data != null)
{
var scheme = data.Scheme;
var host = data.Host;
var args = data.PathSegments;
if (args.Count > 0)
{
var first = args[0];
var second = args[1];
...
}
}
据我所知,上面可以添加到任何activity中,不仅仅是MainActivity
注意事项:
MainLauncher Activity 的 OnCreate 事件将被触发再次。<a href="my.special.scheme://host/arg1/arg2">,在上面 last 代码中,sn-p 值将是:scheme: my.special.scheme
host: host
args: ["arg1", "arg2"]
first: arg1
second: arg2
更新:如果 android 为您的应用创建新实例,您也应该添加 android:launchMode="singleTask"。
【讨论】:
截至 2014 年 1 月 28 日,以上所有答案都不适用于 CHROME
我的应用从 http://example.com/someresource/ 链接正确启动,来自环聊、gmail 等应用的链接,但不是从 chrome 浏览器中启动。
要解决这个问题,以便它从 CHROME 正确启动,您必须像这样设置意图过滤器
<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="example.com"
android:pathPrefix="/someresource/"
android:scheme="http" />
<data
android:host="www.example.com"
android:pathPrefix="/someresource/"
android:scheme="http" />
</intent-filter>
注意pathPrefix 元素
现在,只要用户通过点击 google 搜索结果或任何其他网站的链接从 chrome 浏览器请求 http://example.com/someresource/ 模式,您的应用就会出现在活动选择器中
【讨论】:
截至 2019 年 6 月 15 日
我所做的是包括所有四种打开 url 的可能性。
即,http / https 和 2 带有 www 前缀和 2 不带 www
通过使用它,我的应用程序现在会自动启动,而无需我选择浏览器和其他选项。
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https" android:host="example.in" />
<data android:scheme="https" android:host="www.example.in" />
<data android:scheme="http" android:host="example.in" />
<data android:scheme="http" android:host="www.example.in" />
</intent-filter>
【讨论】:
example.php:
<?php
if(!isset($_GET['app_link'])){ ?>
<iframe src="example.php?app_link=YourApp://blabla" style="display:none;" scrolling="no" frameborder="0"></iframe>
<iframe src="example.php?full_link=http://play.google.com/xyz" style="display:none;" scrolling="no" frameborder="0"></iframe>
<?php
}
else { ?>
<script type="text/javascript">
self.window.location = '<?php echo $_GET['app_link'];?>';
window.parent.location.href = '<?php echo $_GET['full_link'];?>';
</script>
<?php
}
【讨论】:
Felix 处理深层链接的方法是处理深层链接的典型方法。我还建议查看这个库来处理您的深层链接的路由和解析:
https://github.com/airbnb/DeepLinkDispatch
您可以使用注释为特定的深度链接 URI 注册您的 Activity,它会为您提取参数,而无需执行获取路径段、匹配它等通常的繁琐操作。您可以简单地注释和像这样的活动:
@DeepLink("somePath/{someParameter1}/{someParameter2}")
public class MainActivity extends Activity {
...
}
【讨论】:
以下链接提供了有关直接从浏览器启动应用程序(如果已安装)的信息。否则直接在play store打开应用,方便用户无缝下载。
【讨论】:
请注意,当您实现此功能时,如果您的图标从 android 启动器中消失,则您必须拆分意图过滤器。
<activity
android:name=".MainActivity"
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>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="your-own-uri" />
</intent-filter>
</activity>
【讨论】:
是的,Chrome 搜索而不是寻找方案。如果您想通过 URI 方案启动您的应用程序,请在 Play 商店中使用这个很酷的实用程序应用程序。它拯救了我的一天:) https://play.google.com/store/apps/details?id=com.naosim.urlschemesender
【讨论】:
请在此处查看我的评论:Make a link in the Android browser start up my app?
我们强烈劝阻人们不要使用他们自己的方案,除非他们正在定义一个新的全球互联网方案。
【讨论】:
就我而言,我必须为<intent-filter> 设置两个类别,然后它就起作用了:
<intent-filter>
<data android:scheme="my.special.scheme" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
</intent-filter>
【讨论】:
还应该在意图过滤器中添加<category android:name="android.intent.category.BROWSABLE"/>,以使活动从链接中正确识别。
【讨论】:
您需要在 CALLBACK_URL 中添加一个伪主机名“app://”作为 URL 没有意义,无法解析。
【讨论】:
嘿,我找到了解决方案。我没有将类别设置为“默认”。我也将主要活动用于意图数据。现在我对意图数据使用不同的活动。谢谢您的帮助。 :)
【讨论】:
使用<intent-filter> 和<data> 元素。例如,要处理所有指向 twitter.com 的链接,您可以将其放在 <activity> 中的 AndroidManifest.xml 中:
<intent-filter>
<data android:scheme="http" android:host="twitter.com"/>
<action android:name="android.intent.action.VIEW" />
</intent-filter>
然后,当用户在浏览器中点击 twitter 链接时,他们将被询问使用哪个应用程序来完成操作:浏览器或您的应用程序。
当然,如果您想在您的网站和应用之间提供紧密集成,您可以定义自己的方案:
<intent-filter>
<data android:scheme="my.special.scheme" />
<action android:name="android.intent.action.VIEW" />
</intent-filter>
然后,在您的网络应用程序中,您可以放置如下链接:
<a href="my.special.scheme://other/parameters/here">
当用户点击它时,您的应用程序将自动启动(因为它可能是唯一可以处理 my.special.scheme:// 类型的 uris 的应用程序)。唯一的缺点是,如果用户没有安装应用程序,他们会得到一个讨厌的错误。而且我不确定有什么方法可以检查。
编辑:要回答您的问题,您可以使用getIntent().getData(),它返回一个Uri 对象。然后您可以使用Uri.* 方法来提取您需要的数据。例如,假设用户点击了指向http://twitter.com/status/1234的链接:
Uri data = getIntent().getData();
String scheme = data.getScheme(); // "http"
String host = data.getHost(); // "twitter.com"
List<String> params = data.getPathSegments();
String first = params.get(0); // "status"
String second = params.get(1); // "1234"
您可以在Activity 中的任何位置执行上述操作,但您可能希望在onCreate() 中执行此操作。您也可以使用params.size() 来获取Uri 中的路径段数。查看 javadoc 或 android 开发者网站,了解可用于提取特定部分的其他 Uri 方法。
【讨论】:
android:exported="true" 添加到他们的Activity。检查这个答案stackoverflow.com/a/13044911/1276636