Search code examples
androidsmsmessage

how to intent to native app after sending message from inbuilt message app


Am using inbuilt message application in my app to send message, but after sending message it stuck there only,while sending message it should intent back to my application how can i do it.

 public void sendSmsIntent(String phoneNumber) {
            Intent intent2 = new Intent(Intent.ACTION_SENDTO);
            intent2.setData(Uri.parse("smsto:" + Uri.encode(phoneNumber)));
            startActivity(intent2);

        }

Solution

  • Call sendSMS() method wherever you want to send SMS;

     private void sendSMS()
            {
    
                PendingIntent sent = this.createPendingResult(SENT, new Intent(),   PendingIntent.FLAG_UPDATE_CURRENT);
    
    
                SmsManager smsManager = SmsManager.getDefault();
                smsManager.sendDataMessage(strcontactnumber, null, SMS_PORT, messageText.getBytes(), sent, null);
            }
    

    onActivityResult();

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        String toast = null;
        switch (requestCode)
        {
        case SENT :
            switch (resultCode)
            {
            case RESULT_OK :
                toast = "OK";
                break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE :
                toast = "Generic Failure";
                break;
            case SmsManager.RESULT_ERROR_RADIO_OFF :
                toast = "Radio Off";
                break;
            case SmsManager.RESULT_ERROR_NULL_PDU :
                toast = "Null Pdu";
                break;
            }
            break;
        }
    
        if (toast != null)
        {
            Toast.makeText(this, toast, Toast.LENGTH_LONG).show();
        }
    }
    

    It will directly send sms from your app without calling native app

    And if you want to return back to your app by your code then
    
    It is possible. Just need to add the following extra to your intent:
    
    sendIntent.putExtra("exit_on_sent", true);