2012-07-14 81 views
0

服务A将一些信息保存在字符串数组“information []”中。当服务A完成时,它应该启动服务B,并且服务B应该接收字符串数组。如何才能做到这一点? 我试过在捆绑中发送它,但似乎无法像我通常在活动之间那样获取数据。服务之间传递数据(android)

编辑:

这里是我的代码:

服务A,的onDestroy

Intent is2 = new Intent(this, GatherInformationService.class); 
is2.putExtra("information", info); 
startService(is2); 

服务B的onCreate

Bundle b = new Bundle(); 
coords = b.getStringArray("information"); 

回答

0

的OOP招around.You可以让你的静态数组你可以在onCreate方法上访问该数组,或者在Cl的另一个服务中访问你想要的任何地方assName.array = Service1.array

感谢

+1

使用此变通办法很危险 – 2012-07-14 17:56:04

+0

没有危险,我们在大多数情况下使用应用程序变量而不是实例变量。谢谢 – SALMAN 2012-07-14 18:03:00

+1

即使服务A已被销毁? – Eric 2012-07-14 18:03:16

1

您可以使用intent.putExtra方法:

在服务A:

Intent intent = new Intent(this, YourTargetService.class); 
intent.putExtra("YourExtraName", information); 

在服务B的onCreate:

String[] received = getIntnet().getStringArrayExtra("YourExtraName"); 

这应该做的工作

+0

这正是我所做的,但它找不到字符串数组。 – Eric 2012-07-14 18:01:11

2

我看不出你的字符串数组应该如何存在于你创建的新的Bundle对象中。看来,你应该把你的逻辑为onStartCommand方法是这样的:

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    String[] info = intent.getStringArrayExtra("information"); 
} 

请参阅开发者指南的section了解详情。

+0

但onCreate之后不会调用onStartCommand?不是主代码应该在onCreate? – Eric 2012-07-14 19:33:02

+0

每当您创建或不创建服务时,都会调用onStartCommand'。这种方法应该处理该呼叫。相反,'onCreate'对于初始化例程更方便。 – 2012-07-14 19:35:08

+0

@Eric我可以帮你吗? – 2012-07-17 09:50:28