Skip to content

Instantly share code, notes, and snippets.

@Try64
Last active May 1, 2021 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Try64/6776c6e9a545f6cb04dda9d07f167421 to your computer and use it in GitHub Desktop.
Save Try64/6776c6e9a545f6cb04dda9d07f167421 to your computer and use it in GitHub Desktop.
JAVA Method to open SMS app with Data or Show Toast on Error for Android
public void openSMSClientWithData(@NotNull String phoneNumberWithCountryCode,
@NotNull String smsBody,
@NotNull Context contextForToastMessage) {
Uri smsUri = Uri.parse("smsto:" + phoneNumberWithCountryCode);
Intent smsIntent = new Intent(Intent.ACTION_SENDTO, smsUri);
smsIntent.putExtra("sms_body", smsBody);
try {
startActivity(smsIntent);
} catch (Exception ex) {
Toast.makeText(contextForToastMessage, "Error! \n You do not have any SMS App", Toast.LENGTH_LONG).show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment