问题
I am wanting my app to, on a button click, launch the user's default texting app. The intention is not to have the user send a message, but to view their current text conversations, therefore I don't want it to launch the SMS app's "New Message" activity but instead the main app's activity itself.
If I do the following:
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setData(Uri.parse("sms:"));
context.startActivity(sendIntent);
Then this is launched, instead I want it to launch the main part of the app, not this new message screen:
回答1:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setType("vnd.android-dir/mms-sms");
startActivity(intent);
This may be what you want.
回答2:
String defaultApplication = Settings.Secure.getString(getContentResolver(), "sms_default_application");
PackageManager pm = getPackageManager();
Intent intent = pm.getLaunchIntentForPackage(defaultApplication );
if (intent != null) {
startActivity(intent);
}
回答3:
After a long search, this is what I ended up using:
String defaultSmsPackage = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
? Telephony.Sms.getDefaultSmsPackage(this)
: Settings.Secure.getString(getContentResolver(), "sms_default_application");
Intent smsIntent = getPackageManager().getLaunchIntentForPackage(defaultSmsPackage);
if (smsIntent == null) {
smsIntent = new Intent(Intent.ACTION_MAIN);
smsIntent.addCategory(Intent.CATEGORY_DEFAULT);
smsIntent.setType("vnd.android-dir/mms-sms");
}
try {
startActivity(smsIntent);
} catch (Exception e) {
Log.w(TAG, "Could not open SMS app", e);
// Inform user
Toast.makeText(
this,
"Your SMS app could not be opened. Please open it manually.",
Toast.LENGTH_LONG
).show();
}
It supports version KitKat and above. Below that it only supports apps that accept the vnd.android-dir/mms-sms
intent. In case it is impossible to open the app, the user should be notified.
回答4:
Try This:
String number = "12346556"; // The number on which you want to send SMS
startActivity(new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", number, null)));
来源:https://stackoverflow.com/questions/30471461/launch-default-sms-app-without-a-message