ANDROID:如何收到有关短信发送的通知?

时间:2014-03-12 05:41:02

标签: android android-intent telephonymanager

当手机发送短信并在发送短信后获取接收方的号码时,我是否可以获得通知。我使用BroadcasrReceiver获取传入消息的通知。但同样地,我无法使用“android.provider.Telephony.SMS_SENT”跟踪传出的SMS消息

这是我的代码:

这是收到短信时的通知:

public class IncomingSms extends BroadcastReceiver {

    // Get the object of SmsManager
    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 phoneNumber = currentMessage.getDisplayOriginatingAddress();

                    String senderNum = phoneNumber;
                    String message = currentMessage.getDisplayMessageBody();

                    Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);


                   // Show Alert
                    int duration = Toast.LENGTH_LONG;
                    Toast toast = Toast.makeText(context, 
                                 "senderNum: "+ senderNum + ", message: " + message, duration);
                    toast.show();

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

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

        }
    }    
}

我曾经发过短信:

public class MainActivity extends Activity {

    EditText e1,e2;
    Button b;
    String sms,phoneNo;




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


        e1=(EditText) findViewById(R.id.editText1);
        e2=(EditText) findViewById(R.id.editText2);

        b=(Button) findViewById(R.id.button1);

        b.setOnClickListener(new OnClickListener() {




            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub





                sms=e1.getText().toString();
                //sms=e1.getText().toString();

                phoneNo=e2.getText().toString();



                SmsManager smsManager = SmsManager.getDefault();
                smsManager.sendTextMessage(phoneNo, null, sms, null, null);



                Toast.makeText(MainActivity.this, "DelivereD", Toast.LENGTH_LONG).show();




            }
        });



    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;



    }

}

我需要在发送短信后收到通知。 Plzzz帮助我。在此先感谢:)

1 个答案:

答案 0 :(得分:0)

void sendTextMessage(String destinationAddress,String scAddress,String text,PendingIntent sentIntent,PendingIntent deliveryIntent) 是您想要使用的完整方法。 在DeliveryIntent中,即方法的第五个争论,而不是保持空,你可以使用它来做你想做的事情。 创建一个意图,例如,启动另一个活动,一个意图在pendingIntent中的包装,并在方法中使用它。 希望它有所帮助!