安卓:通过开展活动或服务传递复杂的数据结构 [英] Android: Pass complex data structures through activities or services
问题描述
您好我想从我的服务类传递复杂的数据结构来Activity类。
Hi I want to pass complex data structures from my Service class to Activity class.
我创建类序列化:
class SaveMe implements Serializable {
private static final long serialVersionUID = 1L;
static final int test = 1234;
private int user;
private String name;
private double score;
public SaveMe() {}
}
和我把它放在包
Bundle bundle = new Bundle();
bundle.putSerializable("serializable", new SaveMe());
我如何将这些信息发送到活动类?
HOW can I send this information to Activity class ?
我尝试这个code:
Intent mapIntent = new Intent(INTENT_MAP);
mapIntent.putExtras(bundle);
和比我想用传送给BroadcastReciver:
And than I want to send to BroadcastReciver using:
sendBroadcast(mapIntent);
但是我收到的力量错误!
But I receive force error!
我应该使用广播接收器,以这种数据结构传递给我的活动?
Should I use BroadcastReceiver to pass this data structure to my activity?
如何发送到另一个活动?
How to send to another activity?
感谢
推荐答案
您可以通过存储扩展应用程序类,并包括一个getter返回存储在那里的现有SaveMe全球范围内所需的任何数据。您可以从您的活动与
You can store any data required globally by extending the Application class and including a getter to return the existing SaveMe stored there. You can access this from your activity with
private MyApplication application;
@Override
public void onCreate(final Bundle savedInstanceState) {
........
this.application = (MyApplication) this.getApplication();
......
}
// elsewhere in your code
SaveMe mySaveMe = application.getSaveMe();
这篇文章解释得很清楚如何做到这一点: HTTP://www.screaming-penguin。 COM /节点/ 7746 你可以忽略的东西有关AsyncTask的(虽然我敢肯定,你会发现在某些时候需要的),只是集中在部分有关扩展应用程序类。
This article explains very clearly how to do this: http://www.screaming-penguin.com/node/7746 You can ignore the stuff about the AsyncTask (although I'm sure you will find a need for that at some point) and just concentrate on the part about extending the application class.
这篇关于安卓:通过开展活动或服务传递复杂的数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!