我正在尝试使用contentresolver从我的收件箱中读取短信。我可以读取特定号码的短信实际上我发送和接收短信。
mUri = Uri.parse(“content:// sms /”); mContentResolver = pContext.getContentResolver();
我使用上面的代码并从特定数字
获取所有短信请理解我的要求如下: 1.我应该为收件箱中已收到的特定发件人获取短信 2.目前,我正在获取特定号码的所有短信对话,而我只想从收件箱中获取特定号码的短信 3.当我从收件箱收到短信时,从最新到旧显示,我想在列表中显示从旧到最新的短信
提前致谢
答案 0 :(得分:0)
你可以使用这个课程
公共类SmsReceiver扩展了BroadcastReceiver {
String specificPhoneNumber = "No you want";
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
//---retrieve the SMS message received---
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]);
String phNum = msgs[i].getOriginatingAddress();
str += msgs[i].getMessageBody().toString();
if (specificPhoneNumber.equals(phNum))
{
Uri uri = Uri.parse("content://sms/inbox");
ContentResolver contentResolver = context.getContentResolver();
String where = "address="+phNum;
Cursor cursor = contentResolver.query(uri, new String[] { "_id", "thread_id"}, where, null,
null);
while (cursor.moveToNext()) {
long thread_id = cursor.getLong(1);
where = "thread_id="+thread_id;
Uri thread = Uri.parse("content://sms/inbox");
context.getContentResolver().delete(thread, where, null);
}
Intent l = new Intent(context,AgAppMenu.class);
l.putExtra("msg",str);
l.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(l);
}
}
}
}
}
我希望解决你的问题:)