简体   繁体   English

通过后台活动传递数据

[英]passing data through activities in background

I am working on database for a game. 我正在为游戏开发数据库。

I would like to get the score from one activity and call it in database activity, where it will be updated in the database by using intent, the game gets paused when intent is fired. 我想从一个活动中获取分数,并在数据库活动中调用它,该分数将通过使用intent在数据库中进行更新,当触发intent时,游戏将暂停。

I have also tried using application class (suggestions on that are welcome). 我也尝试过使用应用程序类(欢迎提出建议)。

Is there any way to pass data among activities by firing intent in background so that it does not interrupt the gameplay? 是否有任何方法可以通过在后台触发意图在活动之间传递数据,从而不会打断游戏玩法?

Your database should not be in an Activity. 您的数据库不应在活动中。 An Activity represents one screen (or one use-case) in your application, and should not represent implementation details like a database-class. 活动代表您的应用程序中的一个屏幕(或一个用例),并且不应代表数据库类之类的实现细节。

As an alternative, you can put your database code in a Singleton class that can be accessed from everywhere in the same application. 或者,您可以将数据库代码放在Singleton类中,该类可以从同一应用程序中的任何位置访问。 You can also create the database in the Application's onCreate method and store it in a static field there or just create a separate class that exposes static methods to access the database. 您也可以在应用程序的onCreate方法中创建数据库,并将其存储在该数据库的静态字段中,或者只创建一个单独的类,该类公开用于访问数据库的静态方法。

If you are somehow dead-set on using Intents, you would need to use a database Service instead of an Activity. 如果您在使用Intent方面陷入僵局,则需要使用数据库服务而不是Activity。 This way the current Activity will remain on top when you send the data to the service. 这样,当您将数据发送到服务时,当前活动将保持在最前面。 But that would complicate things a lot compared to a simple static class/singleton approach. 但是,与简单的静态类/单例方法相比,这会使事情复杂得多。

You seem to have a big mix up: There shouldn't be a database activity . 您似乎混为一谈:不应进行database activity You should create a database helper class and make it a member variable in your application class. 您应该创建一个数据库帮助程序类,并使其成为应用程序类中的成员变量。

// inside application class
private static MyDatabaseHelper mMyDB;

public MyDatabaseHelper static getDatabase() {
    if (mMyDB == null) {
        // mInstance should be the application instance, means make the
        // application class a singleton
        mMyDB = new MyDatabaseHelper(mInstance);
        // the constructor should open/create the database
    }
    return mMyDB;
}

With this you can work with your database from everywhere. 这样,您可以在任何地方使用数据库。

With that you can easily update every value from everywhere. 这样,您可以轻松地从任何地方更新每个值。 So no need to fire an intent to pass values around. 因此,无需激发意图来传递值。 Instead just update your database directly. 而是直接直接更新数据库。

There are multiple ways to this. 有多种方法可以做到这一点。 You can use handlers, services, then use setresult which give call back to onactivity result of the parent activity. 您可以使用处理程序,服务,然后使用setresult,它可以回调父活动的onactivity结果。

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
Bundle b = new Bundle();

b.putInt("key", 1);

intent.putExtras(b);

startActivity(intent);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM