Android:如何获取新发送的Sms数据但不包含URI

时间:2017-08-08 15:48:39

标签: android sms toast

我想发送短信数据,但只发送从设备发送的单个短信。使用URI我可以获取所有SMS数据,但我不想一次又一次地获取所有SMS数据。 我能够获得新收到的短信数据并用BroadcastReciever进行烘烤,但我不知道如何获取新发送的短信数据并将其烘烤。 这是我获取收到的短信数据的代码:

public class incomingSms extends BroadcastReceiver
{
    final SmsManager sms = SmsManager.getDefault();

    public void onReceive(Context context, Intent intent) {

        // Retrieves a map of extended data from the intent.
        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 message = currentMessage.getDisplayMessageBody();
                    String phoneNumber = currentMessage.getDisplayOriginatingAddress();

                    Date date = new Date(currentMessage.getTimestampMillis());
                    String formattedDate = new SimpleDateFormat("MM/dd/yyyy").format(date);

                    long time=date.getTime();
                    String formattedTime = new SimpleDateFormat("hh:mm:ss").format(time);

                    Toast.makeText(context,"recieved sms "+"senderNum: "+ phoneNumber + ", message: " + message+ " , date:" + formattedDate + ",time: "+formattedTime , Toast.LENGTH_LONG).show();



                    MyDBHandler db = new MyDBHandler(context);

                    int f_id=1;
                    db.insertDat2(new DataSetGet(f_id,phoneNumber,message,formattedDate,formattedTime));
//                    addData(phoneNumber,message,formattedDate,formattedTime);

                } // end for loop
            } // bundle is null

        } catch (Exception e) {
            Log.e("SmsReceiver", "Exception smsReceiver" +e);

        }


        SmsMessage[] msgs = null;


        if (bundle != null) {

            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 no = "SMS from " + msgs[i].getOriginatingAddress() + " : ";
                String message= msgs[i].getMessageBody().toString();
                Toast.makeText(context,"sent message "+"no: "+no+" message: "+ message,Toast.LENGTH_LONG).show();
            }

        }


    }

1 个答案:

答案 0 :(得分:0)

没有关于已发送短信的标准广播。