I have this code to receive the caller number and do query in side case TelephonyManager.CALL_STATE_RINGING
I already get the caller number but when i want to do query in side the case the app will give me error i don’t know why can any one help me please to solve the problem
this is my code
public class IncomingCallReciever extends BroadcastReceiver {
protected static final String TAG = "SWAJ";
private Context mContext;
private Intent mIntent;
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG ," onCallStateChanged ");
mContext = context;
mIntent = intent;
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
int events = PhoneStateListener.LISTEN_CALL_STATE;
tm.listen(phoneStateListener, events);
}
private final PhoneStateListener phoneStateListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
SilentMeDB databaseHelper = new SilentMeDB(mContext);
SQLiteDatabase db = databaseHelper.getReadableDatabase();
Cursor c = db.query("events",null,null, null, null, null, null);
c.moveToFirst();
while(!c.isAfterLast())
{
try
{
Log.d(TAG, "Message is " + c.getString(c.getColumnIndex("Message") ));
}
catch(Exception e)
{
Log.d("Exp", e.getMessage());
}
c.moveToNext();
} c.close();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
}
super.onCallStateChanged(state, incomingNumber);
}
};
}
I think you want to move query code up into the on receive method. I don’t see any reason to be using a listener here. Besides, the moment your onReceive function ends that listener is going to go away and won’t be listening anymore. In your onReceive you can check the Intent you get to see if the call state is ringing and then conditionally execute your code.