Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 让第三方应用程序直接启动我的活动?_Android - Fatal编程技术网

Android 让第三方应用程序直接启动我的活动?

Android 让第三方应用程序直接启动我的活动?,android,Android,我正在进行一项活动,其他第三方希望通过intents在自己的应用程序中使用该活动 现在,此活动正在通过意图过滤器捕获URL,如下所示: <activity android:name=".MyActivity"> <intent-filter> <action android:name="android.intent.action.VIEW"></action> <category android:name="androi

我正在进行一项活动,其他第三方希望通过intents在自己的应用程序中使用该活动

现在,此活动正在通过意图过滤器捕获URL,如下所示:

<activity android:name=".MyActivity">
  <intent-filter> 
    <action android:name="android.intent.action.VIEW"></action>
    <category android:name="android.intent.category.DEFAULT"></category>
    <category android:name="android.intent.category.BROWSABLE"></category>
    <data android:host="www.mysite.com" android:pathPrefix="/test/" android:scheme="http"></data>
  </intent-filter>
</activity>
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://mysite.com/test/somedata"));
startActivity(intent); 
虽然这会起作用,但这可能不会给他们带来想要的效果,让他们直接从他们的活动跳到我的活动——会出现android选择器对话框,询问他们是想用浏览器还是我的应用程序打开意图数据

我如何让第三方直接调用我的活动,而不像这样广播意图?我想让他们仍然向我传递相同的精确数据方案,但只是让他们直接打开我的活动


谢谢

您很可能需要他们直接呼叫您的活动

Class yourClass = Class.forName("com.yourdomain.yourapp.YourClass");
Intent intent = new Intent(this, yourClass);
如果他们没有可以链接的jar。否则,他们只能使用

Intent intent = new Intent(this, YourClass.class);    
然后放一些额外的东西进去。可浏览意图(以及其他意图)的整个概念是为用户提供查看/使用某物的方式的选择。这与从媒体查看器中单击“共享”时发生的情况类似。整个概念是给他们选择。如果有人想开始你的活动,他们需要明确地调用它

编辑:除非Dalvik类加载器知道您的类(可能不会知道),否则我上面的反射示例将无法直接工作。实际上,您需要明确地告诉VM从外部包加载类。您可以使用以下代码来实现这一点

Context foreignContext = createPackageContext("com.yourdomain.yourapp", Context.CONTEXT_IGNORE_SECURITY | Context.CONTEXT_INCLUDE_CODE);
Class<?> yourClass = foreignContext.getClassLoader().loadClass("com.yourdomain.yourapp.YourClass");
Context foreignContext=createPackageContext(“com.yourdomain.yourapp”,Context.Context_IGNORE_SECURITY | Context.Context_INCLUDE_code);
Class yourClass=foreignContext.getClassLoader().loadClass(“com.yourdomain.yourapp.yourClass”);
既然他们有了类对象,他们就可以像以前一样激发意图了。所以完整的代码是这样的

Context foreignContext = createPackageContext("com.yourdomain.yourapp", Context.CONTEXT_IGNORE_SECURITY | Context.CONTEXT_INCLUDE_CODE);
Class<?> yourClass = foreignContext.getClassLoader().loadClass("com.yourdomain.yourapp.YourClass");
Intent intent = new Intent(this, yourClass);
startActivity(intent);
Context foreignContext=createPackageContext(“com.yourdomain.yourapp”,Context.Context_IGNORE_SECURITY | Context.Context_INCLUDE_code);
Class yourClass=foreignContext.getClassLoader().loadClass(“com.yourdomain.yourapp.yourClass”);
意图=新的意图(这个,你的类);
星触觉(意向);

克里斯·汤普森(Chris Thompson)上述解决方案对我不起作用。这一次:


以防有人遇到与我相同的问题。

好吧,我不明白他们如何在源代码中指定“YourClass”——因为他们不需要访问项目中我的活动的java文件才能这样引用它吗?感谢如果他们无法链接到您的库,则可以使用反射来获取类对象的句柄。”Class.forName(someFullyQualifiedClass)'返回完全限定类名的类对象。他们需要了解您的类,但编译器不需要。我已经用示例代码更新了我的答案。谢谢Chris,这很有效。让第三方按上述方式调用我的活动是不标准的还是坏主意?使用意图还是很好的,我只是想用这样一种方式来定义我的意图,它不会也被浏览器应用程序触发?例如,仍然广播一个意图,但定义它的方式是,没有其他应用程序可能也为它注册?谢谢你说实话,我不确定能不能给你一个关于最佳实践的答案。就个人而言,考虑到你想要实现的目标,我不认为这有什么不对。为了或多或少地保证没有其他人使用您的意图(尽管您实际上不能保证…),您需要定义一个自定义意图。看看这个帖子:祝你好运!好的,谢谢-我只是在玩弄它,似乎我可以在我的意图过滤器中添加一个额外的自定义类别,这样有人可以或多或少地将该类别添加到我的活动中。请确保直接转到我的活动,而不必提示选择(除非有人使用与我相同的唯一类别名称编写自己的活动)。谢谢