Android的BroadcastReceiver类中访问数据库

Android的BroadcastReceiver类中访问数据库

本文介绍了如何在Android的BroadcastReceiver类中访问数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 BroadcastReceiver 类来接收传入的呼叫.我想将传入号码与数据库中的号码进行比较.现在,我不明白如何在 BroadcastReceiver 类中使用数据库.我以这种方式在 onReceive()方法中创建 DBAdapter 类的对象:

I have a BroadcastReceiver class to receive incoming call. I want to compare the incoming number with a numbers from my database. Now I can't understand how to use database in BroadcastReceiver class.I make object of DBAdapter class in onReceive() method in this way:

@Override
public void onReceive(Context context, Intent intent) {

  DBAdapter db = new DBAdapter(contenxt);
  Cursor c = d.getAllData();
  while(c.moveToFirst){
      do{
            Log.v("Data : "+c.getString(2));
        }
        while(c.moveToNext);
  }
}

以上代码段均引发NullPointerException.请有人帮助我实现这一目标.

Above code snippet throws NullPointerException. Please somebody help me to achieve this.

推荐答案

最简单的方法如下,

SQLiteDatabase db;
@Override
public void onReceive(Context context, Intent intent) {
    db = context.openOrCreateDatabase("PhoneDB2", 0, null);
    Cursor cur = db.rawQuery("SELECT * From checkedNumbers", null);

    if(cur.moveToFirst()) {
        do {

        } while(cur.moveToNext())
    }
}

这是解决上述问题的好方法.........

This is the great solution of above problem..................

这篇关于如何在Android的BroadcastReceiver类中访问数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 01:49