Question

i am developing android application.In my application i need to send sms to recipant.If sms is send to recipient it will show toast message and display new activity.I did using SMSManager class but i do not want to use SMSManager in my application.I want to use intent for send sms which gives me default SMS view for sending message. I used intent which send sms successfully but unable call other activity.Please solve my this problem i did lot of R&D but not successfull.

i am attaching source code of send sms and called activity

try {
    String smsSent = "SMS_SENT";
    String smsDelivered = "SMS_DELIVERED";

    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(smsSent), 0);
    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
    new Intent(smsDelivered), 0);

    // Receiver for Sent SMS.
    registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode()) {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS sent", Toast.LENGTH_SHORT).show();
                    Intent i=new Intent(this,Display.class).startActivity(i);
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(getBaseContext(), "Generic failure", Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(getBaseContext(), "No service", Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    Toast.makeText(getBaseContext(), "Null PDU", Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    Toast.makeText(getBaseContext(), "Radio off", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }, new IntentFilter(smsSent));

    // Receiver for Delivered SMS.
    registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode()) {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS delivered", Toast.LENGTH_SHORT).show();
                    break;
                case Activity.RESULT_CANCELED:
                    Toast.makeText(getBaseContext(), "SMS not delivered", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }, new IntentFilter(smsDelivered));


    /*SmsManager smsman=SmsManager.getDefault();
    smsman.sendTextMessage(no, null, data, null, null);*/

    /*  
    // called new activity here
    Intent i=new Intent(MainActivity.this,Display.class);
    startActivity(i);
    Toast.makeText(getApplicationContext(), "SMS sent",Toast.LENGTH_LONG).show();
    */


    Intent smsIntent = new Intent(Intent.ACTION_VIEW);
    smsIntent.setType("vnd.android-dir/mms-sms");
    smsIntent.putExtra("address", "");
    smsIntent.putExtra("sms_body","");
    startActivity(smsIntent);  
} catch(Exception e) {
    Toast.makeText(getApplicationContext(), "SMS faild, please try again.", Toast.LENGTH_LONG).show();      
    e.printStackTrace();
}
Was it helpful?

Solution

You can use onActivityResult method.

edit your code as below:

Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address", "");
smsIntent.putExtra("sms_body","");
startActivityForResult(smsIntent, MY_REQUEST_CODE);//MY_REQUEST_CODE int number

you can then override onActivityResult() and catch that request code (which is self defined in the starting activity) there. This is what it looks like:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == MY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
        // do something useful
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top