很抱歉标题不准确,我不知道如何用英语表达概念。
我想收听并存储任何非数字id发送的传入sms (例如"FACEBOOK“或"VODAFONE”等。
当正常组成的电话号码(如555-010-101和类似号码)向我发送sms时,我通常收听它并将其存储在电话db中。
看起来我不能为那些特殊的身份证做同样的事情。
我的接收器就像网络上的许多其他片段一样:
@Override
public void onReceive(Context context, Intent intent) {
message = "";
contactName = "";
// Get SMS map from Intent
Bundle extras = intent.getExtras();
if ( extras != null ) {
// Get received SMS array
Object[] smsExtra = (Object[]) extras.get(SMS_EXTRA_NAME);
// Get ContentResolver object for pushing encrypted SMS to incoming folder
ContentResolver contentResolver = context.getContentResolver();
assert smsExtra != null;
for (Object aSmsExtra : smsExtra) {
sms = SmsMessage.createFromPdu((byte[]) aSmsExtra);
String body = sms.getMessageBody();
address = sms.getOriginatingAddress();
message = body;
Uri uriConversation = Uri.parse("content://mms-sms/conversations/");
final String[] projection = new String[]{"*"};
final String selection = address;
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address));
Cursor cursor = contentResolver.query(uri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null);
// THREAD _ ID CURSOR
Cursor thread_id_cursor = contentResolver.query(
uriConversation,
projection,
selection,
null,
null
);
if (thread_id_cursor.moveToFirst()) {
thread_id = thread_id_cursor.getString(thread_id_cursor.getColumnIndexOrThrow(THREAD_ID));
}
if (cursor.moveToFirst()) {
contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
}
if (contactName != null && contactName.equals("")) {
contactName = address;
}
entireMessageText.append(sms.getDisplayMessageBody());
}
// Put the sms to database
putSmsToDatabase(contentResolver, sms, entireMessageText.toString());
NotificationHandler handler = new NotificationHandler(context);
handler.build();
}
}我想我遗漏了一些特殊身份证的东西。如果您有任何建议,我们将不胜感激。
发布于 2015-11-01 00:43:49
我使用了这段代码,并且工作正常
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
Bundle bundle = intent.getExtras(); //---get the SMS message passed in---
SmsMessage[] msgs;
String msg_from;
if (bundle != null) {
try {
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]);
msg_from = msgs[i].getOriginatingAddress();
String msgBody = msgs[i].getMessageBody();
if (// Check msg_from ) {
// Do Your Action Hear
}
Log.i("SMS Reciver", msgBody + "====>" + msg_from);
}
} catch (Exception e) {
Log.d("Exception caught", e.getMessage());
}
}
}
}https://stackoverflow.com/questions/33453855
复制相似问题