删除Android中的最新传入消息

时间:2016-03-28 04:49:56

标签: android broadcastreceiver sms smsmanager

我已经完成阅读并收到传入的消息,现在我想在应用程序运行时自动删除来自特定号码(android 4.1-below)的传入消息,但它没有成功。我叫delete_thread();在我的BroadcastReciever上删除5556(我的其他模拟器)的最新传入消息。

这是我的MainActivity类:

public class MainActivity extends Activity {

static TextView SMSm;
static String phoneNumber1;
static String SMSBody1;



public static void setSmsDetails(String phoneNumber, String SMSBody) {
    phoneNumber1 = phoneNumber;
    SMSBody1 = SMSBody;
    SMSm.setText("Sender :" + phoneNumber + "\nMessage : "+ SMSBody1);

}



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    SMSm = (TextView) findViewById(R.id.etSmsBody);
    SMSm.setText("Sender : " + phoneNumber1 + " " + "\nSMS: " +
             SMSBody1);

    //delete_thread("5556");
}

public  void delete_thread(String thread) {
    // TODO Auto-generated method stub
    //String thread = "5556";
    Cursor c = getApplicationContext().getContentResolver().query(
              Uri.parse("content://sms/"),new String[] { 
              "_id", "thread_id", "address", "person", "date","body" }, null, null, null);
    try {
        while (c.moveToNext()) 
          {
            int id = c.getInt(0);
            String address = c.getString(2);
            if (address.equals(thread)) 
                {
             getApplicationContext().getContentResolver().delete(
             Uri.parse("content://sms/" + id), null, null);
          }
    }
}catch (Exception e) {
}   
}
}

这是我的BroadcastReciever类:

public class IncomingSms extends BroadcastReceiver{

final SmsManager sms = SmsManager.getDefault();
@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

    final Bundle bundle = intent.getExtras();

    try {
        if (bundle != null) {
            final Object[] pdusObj = (Object[]) bundle.get("pdus");
             for (int i = 0; i < pdusObj.length; i++) {
                 SmsMessage currentMessage = SmsMessage
                            .createFromPdu((byte[]) pdusObj[i]);
                 String phoneNumber = currentMessage
                            .getDisplayOriginatingAddress();
                 String senderNum = phoneNumber;
                 String message = currentMessage.getDisplayMessageBody();

                 Log.i("SmsReciver", "senderNum: " + senderNum
                            + ", message: " + message);
                   MainActivity.setSmsDetails(senderNum, message);

                    // notifikasi sms                    
                    int duration = Toast.LENGTH_LONG;
                    Toast toast = Toast.makeText(context, "Sender: "
                                + senderNum + ", Message: " + message, duration);
                    toast.show();

                   MainActivity del = new MainActivity();
                   del.delete_thread("5556");

             }
        }
    }catch (Exception e) {
         Log.e("SmsReciver", "Exception smsReciver" + e);

    }
}   }

0 个答案:

没有答案