您的位置:首页 > 移动开发 > Android开发

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
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息