【问题标题】:Writing to database when app is closed关闭应用程序时写入数据库
【发布时间】:2016-11-15 00:21:21
【问题描述】:

我正在使用gcm 进行聊天,并且我有一个onMessageReceived() 方法来接收消息,将它们保存在数据库中,并向用户发送通知。

当应用程序正在运行(或暂停 - 在后台运行)时,这就是我在数据库中存储消息的方式:

private DBHelper mDbHelper;
mDbHelper = new DBHelper(MainApplication.getAppContext());
SQLiteDatabase db = mDbHelper.getWritableDatabase();

getAppContext() 方法是我的主要活动中的一个静态方法,它返回上下文。

这一切都有效。我收到一条消息,成功保存,并收到通知(当应用程序运行时,或在后台)

问题是应用何时关闭。我不能使用MainApplication.getAppContext();,因为应用关闭时没有上下文。

也许我应该以其他方式传递上下文?


更新

如果应用程序关闭,我最终将消息保存在服务器上,当用户打开它时,我从服务器获取它们,从那里删除它们,并将它们保存在用户的设备上。 (就像queue pop 操作...)

如果有更好的方法,请告诉我
查看已接受的答案...

【问题讨论】:

  • 最好用在Service Context

标签: java android sqliteopenhelper


【解决方案1】:

好的,所以 1 年后我再次需要这个,我找到了答案:

原来SQLiteOpenHelper 中有一个静态方法可以在没有上下文的情况下打开数据库:openDatabase()

所以替换这个:

mDbHelper = new DBHelper(MainApplication.getContext());
db = mDbHelper.getWritableDatabase();

用这个:

db = SQLiteDatabase.openDatabase("path/to/database.db", null, OPEN_READWRITE);

静态方法openDatabase() 不需要上下文,因此即使在应用关闭时我们也可以调用它。

【讨论】:

    【解决方案2】:

    创建一个扩展Service 的类,并在onStartCommand() 方法中进行数据库操作。同时启动onMessageReceived中的服务。

    【讨论】:

      【解决方案3】:

      您可以根据需要使用广播接收器。

      首先,创建一个如下所示的广播接收器,

      public class UpdateReceiver extends BroadcastReceiver {
      
          protected UpdateListener listener;
      
          public UpdateReceiver(UpdateListener listener) {
      
              this.listener = listener;
          }
      
          @Override
          public void onReceive(Context context, Intent intent) {
      
              if (intent == null || intent.getExtras() == null) {
                  return;
              }
              if (intent.getBooleanExtra("message", false)) {
                  listener.onMessage(intent.getStringExtra("messageText"));
              } 
          }
      
          public interface UpdateListener {
      
      
              public void onMessage(String message);
      
          }
      }
      

      然后让你的 MainActivity 实现这个接收器,比如

      public class MainActivity implements UpdateReceiver.UpdateListener
      

      你需要注册你的广播接收器,然后你可以在你的 MainActivity 中重写 onMessage 方法,你可以在那里接收你的消息。

      所以你的主要活动看起来像,

      public class MainActivity  implements UpdateReceiver.UpdateListener {
      
      
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
      
              UpdateReceiver  chatMessageReceiver = new UpdateReceiver (this);
                  registerReceiver(chatMessageReceiver, new       IntentFilter("messagereceived"));
      
      
      
          }
      
      @Override
          protected void onMessage( String message) {
      
      //do your DB Operations
      
          }
      
      }
      

      在你的 onMessageReceived 方法中,像这样调用这个广播接收器,

              Intent intent = new Intent();
              intent.setAction("messagereceived");
              intent.putExtra("message", true);
              intent.putExtra("messageText", "your message")
              getApplicationContext().sendBroadcast(intent);
      

      【讨论】:

      • 当应用程序运行时,无论我在做什么活动,我都会收到消息(我使用了LocalBroadcastManager)。我成功保存它们并发送通知。问题仅在应用程序关闭时出现。这会解决问题吗?同样,我确实接收这些消息 - 只是无法保存它们。
      • 检查我编辑的答案,这个广播接收器将在收到消息时触发,您可以将消息保存到数据库,无论应用程序是在后台还是被杀死。
      • 再一次,我已经有了这样的接收器。它正在工作。但我就是不能用DBHelper(Context context)
      • 您是否尝试从您的服务中传递 getBaseContext()?
      • 是的。在DBHelper(Context context)super(context, DB_NAME, null, DB_VERSION); 上一直失败
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-17
      • 2015-10-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多