android 短信:电信运营商给你的号码可能是+86开头
2012-01-17 16:42
363 查看
源码中的注意点即:电信运营商给你的号码可能是+86开头的,这个时候去与通讯录中库中的数据匹配,如果不做模糊匹配是不行的,而一些号码更是有‘-’在数字中间。
/**
* This service essentially plays the role of a "worker thread", allowing us to
* store incoming messages to the database, update notifications, etc. without
* blocking the main thread that SmsReceiver runs on.
*/
public class SmsReceiverService extends Service
/**
* This service essentially plays the role of a "worker thread", allowing us to
* store incoming messages to the database, update notifications, etc. without
* blocking the main thread that SmsReceiver runs on.
*/
public class SmsReceiverService extends Service
private void handleSmsReceived(Intent intent) { SmsMessage[] msgs = Intents.getMessagesFromIntent(intent); Log.i(TAG, "------ msgs.length() -------" + msgs.length); Uri messageUri = insertMessage(this, msgs); if (messageUri != null) { // MessagingNotification.updateNewMessageIndicator(this, true); MessagingNotification.updateNewMessageIndicatorandNotifyLac(this, true); } voicePrompt(msgs); }
private synchronized void voicePrompt(SmsMessage[] msgs) { Log.d(TAG, "Coming SMS, suggesting the need for reading"); strRead = new StringBuffer(); SmsMessage msg = msgs[0]; if (msg != null) { String address = msg.getOriginatingAddress(); Log.d(TAG, "======Phone Number:" + address + "======" ); if (!TextUtils.isEmpty(address)) { forEachContact(SmsReceiverService.this); String name = getContactsNameByNumber(SmsReceiverService.this, address); if (!TextUtils.isEmpty(name)) { strRead.append(name); } else { String prefix = "+86"; int index = address.indexOf(prefix); if (index != -1) { address = address.substring(index + prefix.length() ) ; } String nameAgain = getContactsNameByNumber(SmsReceiverService.this, address); if (!TextUtils.isEmpty(nameAgain)) { strRead.append(nameAgain); }else { address = address.replace("+86", ""); String fuzzyMatchingName = getContactNumber(SmsReceiverService.this,address); if(!TextUtils.isEmpty(fuzzyMatchingName)) { strRead.append(fuzzyMatchingName); }else { strRead.append(getResources().getString(R.string.phone_num) + "[r1][n1]"+ address + "[r0][n0]"); } } } }else { Log.d(TAG, "address is empty"); } } msgget = Message.obtain(); msgget.what = 1; handler.sendMessageDelayed(msgget, 1000); }
private synchronized String getContactsNameByNumber(Context context, String number) { Log.d(TAG, "look for contact name by number"); ContentResolver cr = context.getContentResolver(); Cursor cursor = cr.query(Contacts.Phones.CONTENT_URI, new String[] { Contacts.Phones.NAME }, Contacts.Phones.NUMBER + " = ?", new String[] { number }, null); if (cursor != null) { int count = cursor.getCount(); if (count > 0) { if (cursor.moveToFirst()) { do { String name = cursor.getString(cursor.getColumnIndex(Contacts.Phones.NAME)); Log.d(TAG, "find name : " + name ); if (name != null) { return name; } } while (cursor.moveToNext()); } } } return null; }
相关文章推荐
- Android手机中获取手机号码和运营商信息
- android代码判断当前手机的手机号码是属于移动、联通、还是电信的
- Android手机中获取手机号码和运营商信息
- Android手机中获取手机号码和运营商信息
- Android获取手机号码,后台发送短信以及监听短信发送结果。
- android根据号码删除短信
- Android手机中获取手机号码和运营商信息
- Android 调用系统发短信界面,给指定号码发短信,并带短信内容
- Android手机中获取手机号码和运营商信息
- Android之插入通话记录,短信箱插入短信,号码薄插入联系人
- Android手机中获取手机号码和运营商信息
- Android 调用系统发送短信界面,预设号码和短信内容
- Android手机中获取手机号码和运营商信息
- android代码判断当前手机的手机号码是属于移动、联通、还是电信的
- android 手机号码运营商判断
- Android获取手机号码和运营商信息的方法
- Android中TextView不能识别电信号码11888的问题。
- 深度分析:Android中Mms设置页面更改短信中心号码流程
- Android之插入通话记录,短信箱插入短信,号码薄插入联系人
- (转)路由器WAN口IP显示为10、100、172开头,网络被电信联通等运营商做了NAT转发