Android:在服务和活动之间传递数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3747448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: Passing data between service and activity
提问by FuegoFingers
So I'm getting really confused on how to do this whole thing and I was hoping someone could break it down for me a little bit.
所以我对如何做这整件事感到非常困惑,我希望有人可以为我分解一下。
I have a service that should always be running, and at certain times it needs to alert the user that a task is to be completed(probably through a notification bar icon). When the user accepts the task, the service needs to look into the local database, construct some non primitive objects, and give them to the activity that it just started.
我有一个应该始终运行的服务,并且在某些时候它需要提醒用户任务即将完成(可能通过通知栏图标)。当用户接受任务时,服务需要查看本地数据库,构造一些非原始对象,并将它们交给刚刚启动的活动。
I have looked all over and gotten very confused as to a proper approach so I have a few questions to help me wrap my head around it.
我已经看遍了,对正确的方法感到非常困惑,所以我有几个问题可以帮助我解决这个问题。
If an activity creates a local SQLite database can the service and activities of that application access that same database later?
Does the service and activity need to be in the same or separate packages? I would think no but for some reason I remember seeing something about this elsewhere.
How would I do the data transmission from the service to the activity? I was thinking a content provider but it seems like there should be an easier way for the service to just hand off the data. Similar to an Intent but for non primitives.
如果活动创建了本地 SQLite 数据库,那么该应用程序的服务和活动以后是否可以访问同一个数据库?
服务和活动是否需要在同一个或不同的包中?我认为不会,但出于某种原因,我记得在其他地方看到过一些关于此的内容。
我将如何进行从服务到活动的数据传输?我在考虑内容提供商,但似乎应该有一种更简单的方法让服务只传递数据。类似于 Intent 但用于非原语。
Thanks in advance.
提前致谢。
采纳答案by magaio
If the Service and Activity classes are in the same package, I believe they should be able to access the same private storage space.
I would put them in the same package since they are part of the same overall application structure.
Bundle the data into an Intent's extras as Key-value pairs and start the activity.
如果 Service 和 Activity 类在同一个包中,我相信它们应该能够访问相同的私有存储空间。
我会将它们放在同一个包中,因为它们是相同的整体应用程序结构的一部分。
将数据作为键值对捆绑到 Intent 的 extras 中并开始活动。
Intent intent = new Intent(this, SecondActivity.class);
Bundle b = new Bundle();
// see Bundle.putInt, etc.
// Bundle.putSerializable for full Objects (careful there)
b.putXXXXX("key", ITEM);
intent.putExtras(b);
startActivity(intent);
// -- later, in Activity
Bundle b = this.getIntent().getExtras();
int i = b.getInt("key");
回答by Kevin Galligan
In the same application, services and activities can access the same Sqlite database. I'd suggest using a sqlite db. For simplicity, set up your service connection as a local, so you don't need the AIDL interfaces and inter-process communication stuff.
在同一个应用程序中,服务和活动可以访问同一个 Sqlite 数据库。我建议使用 sqlite db。为简单起见,将您的服务连接设置为本地,这样您就不需要 AIDL 接口和进程间通信的东西。
From a service, to notify the user, use the NotificationManager. You can pass a serializable object from your service to an intent, then to the activity that gets started, but I'd suggest passing a db ID instead.
从服务中通知用户,使用 NotificationManager。您可以将一个可序列化的对象从您的服务传递给一个意图,然后传递给开始的活动,但我建议改为传递一个 db ID。
We have built a new Android ORM tool that you can use for this. It includes prototype Activity and Service classes that you can use to build your app. I don't have an example app yet that uses the service, but I'll code that one next and get it up ASAP.
我们已经构建了一个新的 Android ORM 工具,您可以使用它。它包括可用于构建应用程序的原型 Activity 和 Service 类。我还没有一个使用该服务的示例应用程序,但我接下来会编写一个代码并尽快启动它。
See...
看...
And my blog...
还有我的博客...
I currently do some pretty heavy db stuff in the app I'm working on. A service checks with a server for updates, and essentially does what you're looking to do. Its all pretty simple stuff.
我目前在我正在开发的应用程序中做了一些非常繁重的数据库工作。服务检查服务器是否有更新,并且基本上执行您想要执行的操作。它都是非常简单的东西。