Android:如何检查 BroadcastReceiver 中的联系人 phone 中是否存在传入的 SMS 号码并且阻止号码不存在
Android : How to Check if incoming SMS number exist in contact phone in BroadcastReceiver and block is number not exist
我想当有来信的号码不在联系人的短信时,然后块中的文本,通过检查是否有号码不在 phone 书中来确定。
我有一个代码检查号码存在,但错误时间我代码移动 class BroadcastReceiver?,以及如何在 onReceive 中调用方法 contactExists?
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;
}
我使用 AsyncTask,但我对调用方法 contactExists 感到困惑,以及如何将号码输入 contactExists,以便识别现有号码
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) {
// TODO Auto-generated method stub
contactExists(params[0]);
return null;
}
public boolean contactExists(Context context, String number){
Uri lookupUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
String[] mPhoneNumberProjection = { ContactsContract.PhoneLookup._ID, ContactsContract.PhoneLookup.NUMBER, ContactsContract.PhoneLookup.DISPLAY_NAME };
Cursor cur = context.getContentResolver().query(lookupUri,mPhoneNumberProjection, null, null, null);
try {
// Add your data
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
}
}
不要在主线程中查询内容提供程序,运行新线程或使用 AsyncTask。
private class Checkcontact extends AsyncTask<String, Void, Boolean> {
private Context context;
public Checkcontact(Context context) {
// TODO Auto-generated constructor stub
this.context = context;
}
@Override
protected Boolean doInBackground(String... strings) {
try {
return contactExists(context, strings[0]);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
// The argument result is return by method doInBackground
@Override
protected void onPostExecute(Boolean result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
// You can handle the result here
}
public boolean contactExists(Context context, String number) throws Exception {
Uri lookupUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
String[] mPhoneNumberProjection = { ContactsContract.PhoneLookup._ID, ContactsContract.PhoneLookup.NUMBER,
ContactsContract.PhoneLookup.DISPLAY_NAME };
Cursor cur = context.getContentResolver().query(lookupUri, mPhoneNumberProjection, null, null, null);
try {
if (cur.moveToFirst()) {
// if contact are in contact list it will return true
return true;
}
} finally {
if (cur != null)
cur.close();
}
// if contact are not match that means contact are not added
return false;
}
}
开始任务:
new Checkcontact(context).execute("123456789");
我想当有来信的号码不在联系人的短信时,然后块中的文本,通过检查是否有号码不在 phone 书中来确定。 我有一个代码检查号码存在,但错误时间我代码移动 class BroadcastReceiver?,以及如何在 onReceive 中调用方法 contactExists?
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;
}
我使用 AsyncTask,但我对调用方法 contactExists 感到困惑,以及如何将号码输入 contactExists,以便识别现有号码
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) {
// TODO Auto-generated method stub
contactExists(params[0]);
return null;
}
public boolean contactExists(Context context, String number){
Uri lookupUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
String[] mPhoneNumberProjection = { ContactsContract.PhoneLookup._ID, ContactsContract.PhoneLookup.NUMBER, ContactsContract.PhoneLookup.DISPLAY_NAME };
Cursor cur = context.getContentResolver().query(lookupUri,mPhoneNumberProjection, null, null, null);
try {
// Add your data
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
}
}
不要在主线程中查询内容提供程序,运行新线程或使用 AsyncTask。
private class Checkcontact extends AsyncTask<String, Void, Boolean> {
private Context context;
public Checkcontact(Context context) {
// TODO Auto-generated constructor stub
this.context = context;
}
@Override
protected Boolean doInBackground(String... strings) {
try {
return contactExists(context, strings[0]);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
// The argument result is return by method doInBackground
@Override
protected void onPostExecute(Boolean result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
// You can handle the result here
}
public boolean contactExists(Context context, String number) throws Exception {
Uri lookupUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
String[] mPhoneNumberProjection = { ContactsContract.PhoneLookup._ID, ContactsContract.PhoneLookup.NUMBER,
ContactsContract.PhoneLookup.DISPLAY_NAME };
Cursor cur = context.getContentResolver().query(lookupUri, mPhoneNumberProjection, null, null, null);
try {
if (cur.moveToFirst()) {
// if contact are in contact list it will return true
return true;
}
} finally {
if (cur != null)
cur.close();
}
// if contact are not match that means contact are not added
return false;
}
}
开始任务:
new Checkcontact(context).execute("123456789");