0

我有两个应用程序。一个应用程序具有 Activity,另一个应用程序是后台服务。

我可以使用隐式意图过滤器从我的活动应用程序访问服务应用程序。

我需要从服务接收结果。

例如:

从活动应用程序,我将启动服务并发送数据。在服务应用程序中,我需要接收数据并进行一些检查,并且需要作为修改结果返回到活动应用程序。

我可以通过putExtra发送,也可以通过getExtra在服务中检索它。我需要从服务返回一个值并在活动应用程序中接收它。

PS:我需要的是,就像我们对完成()onActivityResult()对活动结果所做的那样。

先谢谢各位大师了。。。

4

2 回答 2

2

首先添加一个这样的类:

package com.test.context; //For example
public class MyContext extends Application
{
   //Here you define the attributes to share through the application
   // with setters and getters
}

并在 AndroidManifest.xml 中添加类的路径,在示例中为 com.test.context 所以:

<application android:name="com.test.context.MyContext"
                 android:icon="@drawable/ic_launcher"
                 android:label="@string/app_name">

然后在您的代码中,您可以执行以下操作:

MyContext ctx = (MyContext)getApplication();

而且您将能够在所有应用程序、顺便说一句活动和服务中共享数据,我是在 Tasker 中这样做的,并且工作正常。

于 2012-04-13T14:06:55.913 回答
1

当有一个完美工作的 binder 协议时,为什么要通过意图进行通信。

http://developer.android.com/guide/topics/fundamentals/bound-services.html

如果活动使用“bindService()”启动服务,则该服务将一直运行,直到活动调用“unbindService()”。

于 2012-04-13T14:30:52.870 回答