如何删除android中的特定短信?

时间:2015-07-03 15:39:33

标签: android sms

我尝试开发一个使用Alarm Manager自动删除短信的应用。但我不知道如何实现onReceive()

import java.util.GregorianCalendar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class Text_msg extends Activity {

    EditText phoneNo,message;
    Button send;

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

        phoneNo=(EditText)findViewById(R.id.editText1);
        message=(EditText)findViewById(R.id.editText2);
        send=(Button)findViewById(R.id.button1);
        send.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {

                SendSms();
                scheduleAlarm(view);
                message.setText("");
            }


        });
    }

    private void SendSms() {
        // TODO Auto-generated method stub
        String number=phoneNo.getText().toString();
        String message_text=message.getText().toString();

        SmsManager manager=SmsManager.getDefault();
        manager.sendTextMessage(number, null, message_text, null, null);

    }
    private void scheduleAlarm(View view) {
        Long time = new GregorianCalendar().getTimeInMillis()+1*60*60*1000;
        Intent intentAlarm = new Intent(this, RecevierTxtMsg.class);
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, time, PendingIntent.getBroadcast(this, 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
        Toast.makeText(this, "SMS have been sent. Your request will be done successfully", Toast.LENGTH_LONG).show();
    }

}

请帮我解决源代码。

1 个答案:

答案 0 :(得分:0)

您必须扩展BroadcastReceiver类:

public class RecevierTxtMsg extends BroadcastReceiver {
        public void onReceive(Context context, Intent intent){
               // code for delete the last sent sms: 
               Uri sentURI = Uri.parse("content://sms/sent");
               String[] columns = new String[] { "_id" };
               Cursor c = context.getContentResolver().query(sentURI, columns, null, null, null);
               if(c == null){
                   // error cannot access sms database
                   return;
               }
               // if cursor (c) is not null you have access to sms database
               if(c.moveToLast()){
                     int lastSMSid = c.getInt(0); // 0 is the index of selected column.
                     // now you have the last sent sms id
                     int deletedRows = context.getContentResolver().delete(sentURI, "_id=?", new String[]{Integer.toString(lastSMSid);}
                     c.close(); // this is important
                     // and now check deletedRows value
                     if(deletedRows > 0){
                          // you have removed sms successfully.
                     }
               }
        }
}

在AndroidManifest中,您必须声明此广播:

<receiver android:name=".ReceiverTxtMsg" />