在后台应用程序时从Firebase Messaging Service访问sqlite数据库

时间:2017-04-16 12:34:50

标签: android sqlite firebase-cloud-messaging

每当我收到来自FirebaseMessagingService的消息时,我希望我的应用程序访问sqlite数据库..

 final ArrayList<Album> albums = new ArrayList<>();
    ArrayList<Album> aa = sqlite_data_helper.getAlbums();
    Log.d("ALBUMSSS", aa.size() + "");
    albums.addAll(aa);

这就是我从FirebaseMessagingService

访问数据库的方式
public ArrayList<Album> getAlbums() {
    SQLiteDatabase db = this.getWritableDatabase();

    try {
        Cursor cur = db.rawQuery("SELECT * FROM " + ALBUMTABLE, null);
        Log.d("CURSOR", cur.getCount() + " Values");
        ArrayList<Album> albums = new ArrayList<>();
        Log.d("GETALBUMS", cur.getCount() + "");
        if(cur != null){
            cur.moveToLast();
            while (cur.isBeforeFirst() == false) {
                Album album = new Album();
                album.setId(cur.getString(cur.getColumnIndex(ALBUMID)));
                album.setName(cur.getString(cur.getColumnIndex(ALBUMNAME)));
                Log.d("GETALBUMS", album.getName());
                album.setNumber_of_images(cur.getInt(cur.getColumnIndex(NUMBEROFIMAGES)));
                album.setUnViewedImages(cur.getInt(cur.getColumnIndex(NUMBEROFUNVIEWEDIMAGES)));
                cur.moveToPrevious();
                albums.add(album);
            }
            Log.d("GETALBUMS", "Got All Albums");
            cur.close();
            return albums;
        }else {
            cur.close();
            Log.d("GETALBUMS", "Cursor Null");
            return null;
        }
    } catch (Exception e) {
        Log.d("GETALBUMS", e.getMessage());
        return null;
    }
}

这是我在DatabaseHelper类中访问数据库的方法..但是当app在后台cur.getCount();时,每当我这样做时返回0 ..我知道如果传递了数据消息,则代码在后台运行。其余的代码工作正常,当app在后台时无法访问数据库,在前台时它能够访问数据库。

提前致谢..

0 个答案:

没有答案