Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.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,我在这里读了不少帖子,但我发现的解决方案似乎都不适合我。恐怕我错过了一些显而易见的东西 我有两个Android应用程序。我可以修改两者的代码。 我想从application1向application2发送一个启动应用程序意图(带有一个额外的参数),当application2完成其工作时,它应该向application1发回一个结果意图 据我所知,该文件: 应用程序1 myintent = getPackageManager().getLaunchIntentForPackage(APPLICAT

我在这里读了不少帖子,但我发现的解决方案似乎都不适合我。恐怕我错过了一些显而易见的东西

我有两个Android应用程序。我可以修改两者的代码。 我想从application1向application2发送一个启动应用程序意图(带有一个额外的参数),当application2完成其工作时,它应该向application1发回一个结果意图

据我所知,该文件:

应用程序1

myintent = getPackageManager().getLaunchIntentForPackage(APPLICATION2NAME);
myintent.putExtra(MY_EXTRA_PARAM_NAME, "avalue");
startActivityForResult(myintent, AN_ID);
  • 应使用startActivityForResult()
  • 实现onActivityResult,其中 那么
  • 处理结果代码和数据。接待处
应用程序2

Intent intent = getIntent();  # this is working
String param = intent.getStringExtra(MY_EXTRA_PARAM_NAME) ; # successfully received parameter

Intent rslt_int = new Intent(); # create new intent for response
# alternatively I also tried Intent rslt_int = getIntent(); 
# which also fails
# I also tried to reuse the intent with Intent rslt_int = intent;
String rslt = "I confirm the reception of " + param; # just create an answer
rslt_int.putExtra(MY_EXTRA_RSLT_NAME, rslt);
setResult(RESULT_OK, rslt_int); # this does not fail, 
                                # but app1 never sees the result
finish();
return; 
  • 应使用getIntent()读取意图
  • 创建响应意图
  • 调用setResult()
  • 调用finish()
我就是无法让它工作:application1总是报告活动已取消

是否有人有一个非常简单的示例应用程序来显示发送/接收意图的工作方式

下面是一些代码片段,显示了我的尝试:

应用程序1

myintent = getPackageManager().getLaunchIntentForPackage(APPLICATION2NAME);
myintent.putExtra(MY_EXTRA_PARAM_NAME, "avalue");
startActivityForResult(myintent, AN_ID);
这里是处理程序

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d(TAG, String.format("got result %d for %d ",
        resultCode, requestCode));
   # here I always receive immediately a result code of 0 
   # (which means if I read the doc correctly ("cancelled")
应用程序2

Intent intent = getIntent();  # this is working
String param = intent.getStringExtra(MY_EXTRA_PARAM_NAME) ; # successfully received parameter

Intent rslt_int = new Intent(); # create new intent for response
# alternatively I also tried Intent rslt_int = getIntent(); 
# which also fails
# I also tried to reuse the intent with Intent rslt_int = intent;
String rslt = "I confirm the reception of " + param; # just create an answer
rslt_int.putExtra(MY_EXTRA_RSLT_NAME, rslt);
setResult(RESULT_OK, rslt_int); # this does not fail, 
                                # but app1 never sees the result
finish();
return; 

如果您为第二个应用程序启动显式活动,则此功能有效:

Intent myintent = new Intent(Intent.ACTION_MAIN);
                    myintent.setComponent(new ComponentName(APPLICATION2NAME, APPLICATION2NAME +".MainActivity"));
myintent.putExtra(MY_EXTRA_PARAM_NAME, "avalue");
startActivityForResult(myintent, AN_ID);
假设您的活动名称为MainActivity。在您的代码中,使用此命令启动应用程序2


不同之处在于,与使用包名启动应用程序2不同,您直接指定要从应用程序2启动的活动。

在第二个应用程序(正在启动)中访问此应用程序,清单文件中的活动条目应包含一个意图过滤器。这就是问题吗?谢谢Krunal,我不认为意图过滤器是问题所在。我对它进行了配置,并假设它正在工作,因为调用了应用程序2,并且可以成功地接收额外的参数。这是一种解决方法,对吗?您建议application2只是“启动”application1并发送其相关数据。我在网上找到的示例(但对我来说不起作用)使用了创建意图、调用setResult(RESULT_OK,intent)的所有序列;然后调用finish();必须考虑你的方法会产生哪些潜在的不同行为。这是所给出示例的修复方法。我解释得比汉克斯好很多。我现在明白你的解释了,我有时间去测试。是的,这确实解决了我的问题:-)不知何故,我一定在文档中忽略了(或者可能真的没有提到),startActivityForResult()不适用于launchintent,而只适用于显式意图。不幸的是,我是一个新手,我不能推翻你的回答。