I want to be when there are incoming sms, the number of which is not in the contact, then the text in the block, of course, checking if there is any number, is not in the phone book. I have a code verification number, but a time error, do I move the code in the BroadcastReceiver? class, and how to call the contactExists method on onReceive?
public boolean contactExists(Context context, String number, ContentResolver contentResolver) { Cursor phones = contentResolver.query(ContactsContract.CommonDataKinds.Phone. CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC"); while (phones.moveToNext()){ String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); if(PhoneNumberUtils.compare(number, phoneNumber)){ return true; } } return false; }
I use AsyncTask, but I'm confused by what to call the contactExists method, and how do I enter a number in contactExists to find out what the number of existing
public class SmsReceiver extends BroadcastReceiver { public static int MSG_TPE = 0; private String number; @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(AppConstants.SMS_RECEIVED_ACTION)) { Bundle bundle = intent.getExtras(); SmsMessage[] msgs = null; String message = ""; String date = AppUtils.getDate(); String time = AppUtils.getTime(); String status = AppUtils.getStatus(); if (bundle != null) { Object[] pdus = (Object[]) bundle.get("pdus"); msgs = new SmsMessage[pdus.length]; for (int i = 0; i < msgs.length; i++) { msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]); number = msgs[i].getOriginatingAddress(); message += msgs[i].getMessageBody().toString(); message += "\n"; } if (SettingsPreferences.isBlockAllSms(context)) { this.abortBroadcast(); CallMessageItem item = new CallMessageItem(); item.setDate(date); item.setMessage(message); item.setNumber(number); item.setTime(time); item.setStatus(status); item.setType(AppConstants.TYPE_MESSAGE); CMBDataProvider.addCallMessage(context, item); if (SettingsPreferences.isNotificationShow(context)) { AppUtils.generateNotification( context, context.getResources().getString( R.string.block_sms_message), false); } } else if (SettingsPreferences.isBlockPrivateSms(context)) { ArrayList<BlockItem> block_number = CMBDataProvider .getBlackList(context); if (!TextUtils.isEmpty(message) && !TextUtils.isEmpty(number) && block_number != null && block_number.size() > 0) { message = message.trim(); for (int i = 0; i < block_number.size(); i++) { if (number .contains(block_number.get(i).getNumber())) { this.abortBroadcast(); CallMessageItem item = new CallMessageItem(); item.setDate(date); item.setMessage(message); item.setNumber(number); item.setTime(time); item.setType(AppConstants.TYPE_MESSAGE); CMBDataProvider.addCallMessage(context, item); if (SettingsPreferences .isNotificationShow(context)) { AppUtils.generateNotification( context, context.getResources().getString( R.string.block_sms_message), false); } break; } } } } } } } private class Contactexixt extends AsyncTask<String, Integer, Double> { @Override protected Double doInBackground(String... params) {
source share