中断到我的应用程序的链接 [英] interrupting link to my app

查看:78
本文介绍了中断到我的应用程序的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题.我正在使用以下代码以

I have a problem. I'm using the below code to interrupt links to my app as

<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="http" android:host="twitter.com"/>
    <data android:scheme="http" android:host="facebook.com"/>
</intent-filter>

但是问题是我需要在运行时设置数据方案和主机,即可以在运行时添加或删除主机.如何在运行时设置数据方案和主机的值?我正在使用以下代码,但无法正常工作

But the problem is that I need to set data scheme and host at runtime i.e. I can add or delete the host at runtime. How to set the value of data scheme and host at runtime? I am using below code but it is not working

IntentFilter filter = new IntentFilter();
filter.addAction("android.intent.action.VIEW");
filter.addCategory("android.intent.category.DEFAULT");
filter.addCategory("android.intent.category.BROWSABLE");
filter.addDataScheme("http");
filter.addDataAuthority("www.facebook.com", null);
RecieveBroadcaster  receiver = new RecieveBroadcaster();
registerReceiver(receiver, filter);

推荐答案

严格来说, ACTION_VIEW 对应的字符串是惯例的活动动作;您将其放入清单中 activity intent-filter 元素中的事实,使成为活动动作!系统代表您的应用程序侦听这些,这基本上就是为什么您自己(不能)侦听它们的原因. Context.startActivity()方法生成这些 Intent .

Strictly speaking, the string corresponding to ACTION_VIEW is an activity action by convention; the fact that you place it into the intent-filter element of an activity in your manifest, makes it an activity action! The system listens for these on your application's behalf, which is basically why you don't (can't) listen for them yourself. The Context.startActivity() method generates these Intents.

意图解析规则实际上确定特定的 Intent 是否与任何 IntentFilter 匹配.对于活动意图,可能有多个匹配项,并且通常会显示选择器"界面,因此用户可以选择目标.

The rules of intent resolution actually determine whether a particular Intent matches any IntentFilters. For activity intents, there may be multiple matches, and that usually displays the "Chooser" interface, so the user can select a target.

有3个永不交叉的Intent 流": startActivity() sendBroadcast() startService().这些都是通过 Context 中的方法启动的,每个都有一个特定的目标 Activity BroadcastReceiver Service .

There are three Intent "streams" that never cross: startActivity(), sendBroadcast() and startService(). These are all initiated via methods in Context and each has a specific target Activity, BroadcastReceiver and Service respectively.

设置一个 BroadcastReceiver (不是您甚至没有尝试过该代码的 ReceiveBroadcaster 吗?)来获取您感兴趣的事件,然后使用即可,这很简单. Context.startActivity()和所需的 Intent .您甚至可以使用自定义操作,因此您知道它是由接收者而不是用户触发的.

It is a simple matter to set up a BroadcastReceiver (not ReceiveBroadcaster did you even try that code?) to get the events you are interested in, and then use Context.startActivity() with the Intent you want. You can even use a custom action, so you know it was triggered by the receiver, and not the user.

唯一的问题是:您可以安排接收广播事件吗?您可以通过 Context.sendBroadcast()来注册一个系统事件,或者自己生成一个自定义事件.

The only question is: is there a broadcast event you can arrange to receive? There may be a system event you can register for, or you may be able to generate a custom event yourself, via Context.sendBroadcast().

请记住,您可以检查活动开始时传入的 Intent ,如果与您的活动不完全匹配,则转发"相同或经过修改的 Intent 寻找.如正确确定的那样,您不能动态更改活动的 IntentFilter 集合,因此您将必须检查每个请求的主机.

Remember you can inspect the incoming Intent your activity was started with, and "forward" the same or a modified Intent if it doesn't exactly match what you are looking for. As you correctly determined, you cannot dynamically alter an activity's set of IntentFilters, so you will have to inspect the host of every request.

请记住,您也可以在清单中注册接收者,并让系统自动调用该实现.

Remember you can also register receivers in your manifest as well, and have that implementation called automatically by the system.

这篇关于中断到我的应用程序的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆