Par*_*odi 403 android intentfilter android-intent
任何人都可以指导我如何从Android浏览器启动我的Android应用程序?
Fel*_*lix 605
使用<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>
Run Code Online (Sandbox Code Playgroud)
然后,当用户在浏览器中单击指向twitter的链接时,将询问他们使用哪个应用程序来完成操作:浏览器或您的应用程序.
当然,如果您想在您的网站和应用程序之间提供紧密集成,您可以定义自己的方案:
<intent-filter>
<data android:scheme="my.special.scheme" />
<action android:name="android.intent.action.VIEW" />
</intent-filter>
Run Code Online (Sandbox Code Playgroud)
然后,在您的网络应用程序中,您可以添加以下链接:
<a href="my.special.scheme://other/parameters/here">
Run Code Online (Sandbox Code Playgroud)
当用户点击它时,您的应用程序将自动启动(因为它可能是唯一可以处理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"
Run Code Online (Sandbox Code Playgroud)
你可以在你的任何地方完成上述任务Activity,但你可能会想要这样做onCreate().您还可以使用params.size()获取路径段的数量Uri.查看javadoc或android开发人员网站,了解Uri可用于提取特定部分的其他方法.
And*_*cko 68
CHROME截至2014年1月28日,以上所有答案均不适用于我
我的应用程序从http://example.com/someresource/正确启动了应用程序,例如环聊,gmail等,但不是来自Chrome浏览器.
要解决这个问题,以便从CHROME正确启动你必须像这样设置intent过滤器
<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>
Run Code Online (Sandbox Code Playgroud)
注意pathPrefix元素
只要用户通过点击谷歌搜索结果或任何其他网站上的链接从Chrome浏览器请求http://example.com/someresource/模式,您的应用就会显示在活动选择器中
hac*_*bod 66
请在此处查看我的评论:在Android浏览器中创建一个链接启动我的应用程序?
我们强烈反对人们不使用他们自己的计划,除非他们正在定义一个新的全球互联网计划.
Zak*_*aki 48
在我的情况下,我必须设置两个类别<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>
Run Code Online (Sandbox Code Playgroud)
Ден*_*еня 21
例如,你有接下来的事情:
打开应用的链接:http://example.com
您应用的软件包名称:com.example.mypackage
然后你需要做下一步:
1)为您的活动添加一个意图过滤器(可以是您想要的任何活动.有关更多信息,请查看文档).
<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>
Run Code Online (Sandbox Code Playgroud)
2)创建HTML文件以测试链接或使用此方法.
<a href="intent://example.com#Intent;scheme=http;package=com.example.mypackage;end">Open your Activity directly (just open your Activity, without a choosing dialog). </a>
<a href="http://example.com">Open this link with browser or your programm (by choosing dialog).</a>
Run Code Online (Sandbox Code Playgroud)
3)使用Mobile Chrome进行测试
4)就是这样.
并没有必要在市场上发布应用程序以测试深层链接=)
geo*_*gij 18
还应<category android:name="android.intent.category.BROWSABLE"/>添加到intent过滤器,以便从链接中正确识别活动.
以下链接提供有关直接从浏览器启动应用程序(如果已安装)的信息.否则它会直接在Play商店中打开应用程序,以便用户可以无缝下载.
https://developer.chrome.com/multidevice/android/intents
请注意,当您实现此功能时,如果您的图标从Android启动器中消失,则必须拆分intent-filter.
<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>
Run Code Online (Sandbox Code Playgroud)
是的,Chrome搜索而不是寻找方案.如果要通过URI方案启动应用程序,请在Play商店中使用此酷实用程序App.它节省了我的一天:) https://play.google.com/store/apps/details?id=com.naosim.urlschemesender
Xamarin 费利克斯港的答案
在您的中MainActivity,添加以下内容(docs:Android.App.IntentFilterAttribute Class):
....
[IntentFilter(new[] {
Intent.ActionView },
Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
DataScheme = "my.special.scheme")
]
public class MainActivity : Activity
{
....
Run Code Online (Sandbox Code Playgroud)
Xamarin将为您添加以下内容AndroidManifest.xml:
<activity android:label="Something" android:screenOrientation="portrait" android:theme="@style/MyTheme" android:name="blahblahblah.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>
Run Code Online (Sandbox Code Playgroud)
为了获取参数(我在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];
...
}
}
Run Code Online (Sandbox Code Playgroud)
据我所知,以上内容可以添加到任何活动中,而不仅仅是MainActivity
笔记:
OnCreate应用程序的事件MainLauncher Activity将再次被触发。<a href="my.special.scheme://host/arg1/arg2">,在以上最后一个代码中,代码段值将为:scheme: my.special.scheme
host: host
args: ["arg1", "arg2"]
first: arg1
second: arg2
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
314109 次 |
| 最近记录: |