我使用此代碼從特定的電話號碼獲取SMS消息,然後打開我的應用程序。如何從特定號碼讀取短信並以編程方式從收件箱中刪除
它工作正常,但它不會從收件箱從特定電話號碼獲取短信後刪除短信。
我使用模擬器進行測試,請幫告訴我什麼是在代碼中的錯誤? 如何刪除特定號碼的短信?
public class SmsReceiver extends BroadcastReceiver {
String specificPhoneNumber = "15555215554";
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);
}
}
}
}
}
你找到了解決辦法? –