Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.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
Java Android:从另一个应用程序的服务访问数据_Java_Android - Fatal编程技术网

Java Android:从另一个应用程序的服务访问数据

Java Android:从另一个应用程序的服务访问数据,java,android,Java,Android,有两个应用程序,App1和App2。我在App1中启动了如下服务: Intent sendIntent = new Intent(this, HelloService.class); sendIntent.setAction("getDataFromApp1"); sendIntent.putExtra("dataKey", "This is data I am sending."); startService(sendIntent); Intent sendIntent = new Inte

有两个应用程序,App1和App2。我在App1中启动了如下服务:

Intent sendIntent = new Intent(this, HelloService.class);
sendIntent.setAction("getDataFromApp1");
sendIntent.putExtra("dataKey", "This is data I am sending.");
startService(sendIntent);
Intent sendIntent = new Intent();
sendIntent.setAction(your.custom.action.for.app.two);
sendIntent.putExtra("dataKey", "Pass this text to app two.");
startActivity(sendIntent);
现在我想从该服务中获取数据[我使用
putExtra()
]在Intent中设置的字符串对象],并在App2中显示它


所以我的问题是如何从另一个应用程序创建的服务中访问数据。我已经用android:exported=true在清单文件中声明了该服务。

您需要在App1中公开内容提供者,它提供可在App2中使用的数据。 是的,您需要确保exported设置为true

你可以从这个开始。

您可以通过使用
Intent
实现这一点。您可以将任何想要的数据附加到
意图
,并且可以在任何应用程序中侦听特定类型的
意图

例如,您可以发送一个
意图
,如下所示:

Intent sendIntent = new Intent(this, HelloService.class);
sendIntent.setAction("getDataFromApp1");
sendIntent.putExtra("dataKey", "This is data I am sending.");
startService(sendIntent);
Intent sendIntent = new Intent();
sendIntent.setAction(your.custom.action.for.app.two);
sendIntent.putExtra("dataKey", "Pass this text to app two.");
startActivity(sendIntent);
然后在第二个应用程序中,将意图过滤器附加到您希望在
AndroidManifest.xml中启动的
活动
,如下所示:

<intent-filter>
        <action android:name="your.custom.action.for.app.two" />
</intent-filter>

还有很多其他的事情你需要考虑。比如检查用户手机上是否存在第二个应用程序,如果应用程序未唤醒怎么办?有关更多信息,请参阅。

我已使用App1中的putExtra()将数据放入了intent中。所以我需要在App2中提取该数据。好的,如果App1正在向App2“提供”数据,这应该可以,但是如果App2正在“请求”数据,那么您将需要内容提供商。请重新措辞你的问题。这是一个字符串名还是软件包名这可以是你喜欢的任何东西,它只是一个你需要在另一端匹配的键,就像一个变量名。我想通过一个服务传递数据,而不是一个活动。你可以发送一个广播,它将通过一个服务接收,只需将startActivity切换到sendBroadcast,并将意向过滤器连接到服务。使用“绑定服务”模式,然后我对android非常陌生,我不知道绑定服务,所以了解它,了解更多