android 短信:电信运营商给你的号码可能是+86开头


源码中的注意点即:电信运营商给你的号码可能是+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

	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;
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值