0

folks. I'm making a broadcast receiver to "black list" contacts on android. So I have two forms to add contacts to the blacklist. One of them is adding them manually to a sqlite database, and the other is adding them from the contacts app. When I receive a sms text from a number from the blacklist, my app should catch the event and send a message in response. I got problems when I'm trying to verify the number on the blacklist. I'm fetching the number and using a cursor with a select to verify the existence of the contact. When the cursor check the number added manually I got no problems but when the contact is added from the contact app the cursor returns 0 rows. Note that I talk Spanish so some words are in spanish.

public class RecibirMensaje extends BroadcastReceiver{
    static final String ACTION="android.provider.Telephony.SMS_RECEIVED";
    SmsMessage message;

    String dbName="DBRespuestas";
    String tableRegistro="registro";
    SQLiteDatabase db;

    EditText nombre,telefono,mensaje;
    Context context;
    @Override
    public void onReceive(Context context, Intent intent){
        db=context.openOrCreateDatabase(dbName,Context.MODE_PRIVATE,null);
        this.context=context;
        if (intent.getAction().equals(RecibirMensaje.ACTION)){
            StringBuilder stringBuilder=new StringBuilder();
            String textMessage;

            Bundle bundle=intent.getExtras();
            if (bundle!=null){
                Object[]pdus=(Object[])bundle.get("pdus");
                for (Object pdu:pdus){
                    message= SmsMessage.createFromPdu((byte[]) pdu);
                }
            }
            Toast.makeText(context,message.getDisplayOriginatingAddress().toString(),Toast.LENGTH_LONG).show();
            if (isBlacklisted(message.getDisplayOriginatingAddress())){

            }
        }

    }

    private boolean isBlacklisted(String phone) {
        db=context.openOrCreateDatabase(dbName,Context.MODE_PRIVATE,null);
        Cursor cursor=db.rawQuery("Select nombre, telefono from registro where telefono='"+phone+"'",null);
        cursor.moveToFirst();
        if (cursor.getCount()>=0){
            Toast.makeText(context,Integer.toString(cursor.getCount()),Toast.LENGTH_LONG).show();
        return true;
        }else {
            Toast.makeText(context,"Cursor vacio",Toast.LENGTH_LONG).show();
        return false;
        }

    }
}

I checked if the passing data is correct with no luck, check the data in the database too. Any help is apreciated. Thanks in advance.

Dima Kozhevin
  • 3,602
  • 9
  • 39
  • 52
Kevin
  • 45
  • 1
  • 1
  • 7
  • First Check your registro table for data, If It contains Rows or not. – ADM Oct 23 '17 at 05:59
  • I'd suggest adding something along the lines of `Cursor csr = db.query("registro",null,null,null,null,null,null); while(csr.moveToNext()) { Log.d("TESTDATA","Phone Number="+Integer.toString(csr.getStringcsr.getColumnIndex("phone"))); }` to see if there is any data at all. Add this after the line `db=context.openOrCreate..........` p.s no need for `moveToFirst()`, getCount()` will return the count of rows irrespective of the position within the cursor. – MikeT Oct 23 '17 at 06:04
  • You may also find this of use [Are there any methods that assist with resolving common SQLite issues?](https://stackoverflow.com/questions/46642269/are-there-any-methods-that-assist-with-resolving-common-sqlite-issues). – MikeT Oct 23 '17 at 06:15
  • is there any chance you forgot to add to your manifest . I assume you didn't . you can look into this link https://developer.android.com/training/contacts-provider/retrieve-names.html – Wubbalubbadubdub Oct 23 '17 at 07:00

0 Answers0