@clu 有正确的答案,只是向后大声笑。应该是这样的:
//Create the intent to share and set extras
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
sendIntent.putExtra(Intent.EXTRA_TEXT, message);
sendIntent.setType("text/plain");
//Check if device API is LESS than KitKat
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT)
context.startActivity(sendIntent);
else
context.startActivity(Intent.createChooser(sendIntent, "Share"));
此构建检查也可以缩短为单行:
//Create the intent to share and set extras
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
sendIntent.putExtra(Intent.EXTRA_TEXT, message);
sendIntent.setType("text/plain");
//Check if device API is LESS than KitKat
startActivity(Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT ? sendIntent : intent.createChooser(sendIntent, "Share"));