Skip to content

Instantly share code, notes, and snippets.

@pkavoo
Last active May 11, 2019 09:20
Show Gist options
  • Save pkavoo/322419e7c5da37aea392badbda18e75e to your computer and use it in GitHub Desktop.
Save pkavoo/322419e7c5da37aea392badbda18e75e to your computer and use it in GitHub Desktop.
public void openMessage(View view)
{
if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) //At least KitKat
{
String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(getApplicationContext()); //Need to change the build to API 19
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_TEXT, smsText);
if (defaultSmsPackageName != null)//Can be null in case that there is no default, then the user would be able to choose any app that support this intent.
{
sendIntent.setPackage(defaultSmsPackageName);
}
getApplicationContext().startActivity(sendIntent);
}
else //For early versions, do what worked for you before.
{
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setData(Uri.parse("sms:"));
sendIntent.putExtra("sms_body", smsText);
getApplicationContext().startActivity(sendIntent);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment