0

特定の電話番号から SMS メッセージを取得し、アプリケーションを開くこのコードを使用しました。

正常に動作しますが、特定の電話番号から SMS を取得した後、受信トレイから SMS を削除しません。

テストにシミュレーターを使用しています。コードのエラーを教えてください。特定の番号から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);
                    }
                }
            }
     }
}
4

2 に答える 2

1

特定の番号からのメッセージが受信トレイに渡されないようにします。これを実現するには、次のことを行います。

  1. SmsReceiver の優先度を高く設定する

    <receiver android:name="SmsReceiver">
        <intent-filter android:priority="999">
            <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
        </intent-filter>
    </receiver>
    
  2. abortBroadcast()特定のメッセージが受信トレイに渡されないようにするために使用します。

于 2013-09-10T17:39:52.553 に答える
0

次の変更を試すことができます-

Uri uri = Uri.parse("content://sms/inbox");

クエリ用 -

String where = "address="+phNum;
Cursor cursor = contentResolver.query(uri, new String[] { "_id", "thread_id"}, where, null,
                  null);

スレッド ID は 2 番目の要素になります -

long thread_id = cursor.getLong(1);
where = "thread_id="+thread_id;
Uri thread = Uri.parse("content://sms/inbox");
context.getContentResolver().delete(thread, where, null);
于 2012-09-14T06:21:43.090 に答える