Cursor is Returning zero when called from Broadcas

2019-08-31 23:25发布

问题:

I am getting the cursor value zero when i access it from Broadcast Receiver class , this is the code which i have use to access value from database

public Cursor select_in(String num)
{
   final Cursor cur= db.rawQuery("SELECT incoming FROM contactlist WHERE number=\""+num+"\"",null);
   return cur;
}

and this is how I call the function from my Broadcast Receiver Class and i have passed the context of OnReceive Method.

openhelper=new OpenHelper(context);

this.db=openhelper.getWritableDatabase();
this.dh=new DataHelper(context);
String num=phonenumber;
Cursor cur=this.dh.select_in(num);  
if (cur.moveToFirst()){

do{
   incoming=cur.getInt(cur.getColumnIndex("incoming"));
}while(cur.moveToNext());
}

My operations are not so heavy that it will not get completed within 10 sec as I am using it in broadcast Receiver, I have also closed the database and also the cursor ,please can any one suggest me how to get that value . Thanks In Advance.

回答1:

Cursor c ;
         try{
            c = theDatabase.query("data", new String[] {KEY_CITY, KEY_TIMEZONE, KEY_COUNTRY, KEY_TIMEZONE_DISPLAY_NAME, KEY_TIMEZONE_ID},_ID+" = '"+id+"';", null, null, null, null, null);
         }catch(Exception e){
                Log.e("CLOCK", "Error getting Timezone for "+KEY_CITY+" : "+e.getMessage());
                return null;
        }