2

我想删除特定手机号码中的消息。在安卓手机中。为此,我使用以下代码..

此代码中的问题是它正在删除手机中的所有对话。所以请帮我解决这个问题。提前谢谢...

public class MainActivity extends Activity

{

@Override
protected void onCreate(Bundle savedInstanceState)
{
//  String specificPhoneNumber = "+91" ;

    super.onCreate(savedInstanceState);
    long V;
    MainActivity thr = new MainActivity();
    V =thr.getThreadId(thr);
    Uri thread = Uri.parse("content://sms/inbox/8767564523" + V);


        getContentResolver().delete(thread, null, null);


}


private long getThreadId(Context context)
    {
        long threadId = 0;

        String SMS_READ_COLUMN = "read";
        String WHERE_CONDITION = SMS_READ_COLUMN + " = 0";
        String SORT_ORDER = "date DESC";
        int count = 0;
        Uri uri1 = Uri.parse("content://sms/inbox/");
        Cursor cursor = context.getContentResolver().query(uri1,new String[] { "_id", "thread_id", "address", "person", "date", "body" },WHERE_CONDITION,null,SORT_ORDER);

        if (cursor != null) {
                try {
                    count = cursor.getCount();
                    if (count > 0) {
                        cursor.moveToFirst();
                        threadId = cursor.getLong(1);                              
                    }
                } finally {
                        cursor.close();
                }
        }


        return threadId;
    }

}

4

1 回答 1

3

哦...知道了..它正在 100% 工作,

public void onReceive(Context context, Intent intent) {
            //Bundle bundle = intent.getExtras();

           // if(null == bundle)
             //       return;
          //  SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);       
            try {
                // Java reflection to gain access to TelephonyManager's
                // ITelephony getter
                tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                Log.v("1","BYE BYE BYE" );
                Class c = Class.forName(tm.getClass().getName());
                Method m = c.getDeclaredMethod("getITelephony");
                m.setAccessible(true);
               // com.android.internal.telephony.ITelephony 
                telephonyService = (ITelephony) m.invoke(tm);  
                Bundle b = intent.getExtras();
                incommingNumber = b.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
                Toast.makeText(context, incommingNumber, Toast.LENGTH_LONG).show();
                Log.v("2","BYE BYE BYE" );
                for(int i=0;i<1;i++)
                {
                if ( incommingNumber.equals(BINO[i]) )
                {

                }
                else{
                    Toast.makeText(context,incommingNumber, Toast.LENGTH_LONG).show();
                     telephonyService = (ITelephony) m.invoke(tm);
                    //telephonyService.silenceRinger();
                    telephonyService.endCall();
                    Log.v("3","BYE BYE BYE" );

                //telephonyService.answerRingingCall();

                }
                }


            } catch (Exception e) {
                e.printStackTrace();


            }
        }
     };//BroadcastReceiver
    /**
     * Broadcast receiver to detect the outgoing calls.
     */
    public class OutgoingReceiver extends BroadcastReceiver 
    {
        public OutgoingReceiver() {
        }

        @Override
        public void onReceive(Context context, Intent intent) 
        {
            String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
       //     setResultData(null);
           int c = (number).indexOf("YOUR NO...");
           Toast.makeText(context, String.valueOf(c), Toast.LENGTH_LONG).show();
           for(int i =0;i<2;i++)
           {

           if ((number).indexOf(BONO[i])!= -1)//||number.contentEquals("+917204302689")||number.contentEquals("07204302689"))
            {
                setResultData(null);
                Toast.makeText(context, "This call is not allowed!", Toast.LENGTH_LONG).show();
            }
           }
        }

    }

    public class MessageReceiver extends BroadcastReceiver
    {
        public void onReceive(Context context, Intent intent) 
         {
             Bundle bundle = intent.getExtras();
             //Bundle bundle = intent.getExtras();


                if ( bundle != null )
                {
                   // do you manipulation on String then if you can abort.



                }
                Object messages[] = (Object[]) bundle.get("pdus");
                SmsMessage smsMessage[] = new SmsMessage[messages.length];

                for (int n = 0; n <messages.length; n++) 
                {
                smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
                }
                for(int i=0;i<1;i++)
                {
                if(MINO[i].equalsIgnoreCase(smsMessage[0].getOriginatingAddress()))
                {
                    //abortBroadcast();
                    String Body=smsMessage[0].getMessageBody();
                    if (Body.startsWith("START"))
                    {
                        Toast toast4 = Toast.makeText(context,"There is START ", Toast.LENGTH_LONG);toast4.show(); 
                        abortBroadcast();
                    }
                }
                else
                {

                    abortBroadcast();   
                }
                }
                // show first message
                Toast toast1 = Toast.makeText(context,"Received SMS: " + smsMessage[0].getOriginatingAddress()+ "\nBody: "+smsMessage[0].getMessageBody(), Toast.LENGTH_LONG);
                toast1.show(); 

         }
         };//BroadcastReceiver

    String incommingNumber;
    private Context ctx;
    private TelephonyManager tm;
    //private CallStateListener callStateListener;
    ITelephony telephonyService;
    private OutgoingReceiver outgoingReceiver;
    private IncomingReceiver incomingReceiver;
    private  MessageReceiver  messageReceiver;

    public CallHelper(Context ctx) 
    {
        this.ctx = ctx;

        //callStateListener = new CallStateListener();
        outgoingReceiver  = new OutgoingReceiver();
        incomingReceiver  = new IncomingReceiver();
        messageReceiver  = new MessageReceiver();
    }

    /**
     * Start calls detection.
     */
    public void start() {
        //tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
        //tm.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);

        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_NEW_OUTGOING_CALL);
        ctx.registerReceiver(outgoingReceiver, intentFilter);

         IntentFilter filter= new IntentFilter("android.intent.action.PHONE_STATE");
         ctx.registerReceiver(incomingReceiver, filter);

         IntentFilter filter1= new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
         ctx.registerReceiver(messageReceiver, filter1);
    }

    /**
     * Stop calls detection.
     */
    public void stop() 
    {
        //tm.listen(callStateListener, PhoneStateListener.LISTEN_NONE);
        ctx.unregisterReceiver(outgoingReceiver);
        ctx.unregisterReceiver(incomingReceiver);
        ctx.unregisterReceiver(messageReceiver);
    }

}
于 2013-04-17T08:10:48.490 回答