启动默认的SMS应用程序,无需消息

时间:2015-05-27 01:02:39

标签: android android-intent text sms message

我希望我的应用程序能够在按钮单击时启动用户的默认短信应用程序。目的不是让用户发送消息,而是查看他们当前的文本对话,因此我不希望它启动SMS应用程序的“新消息”活动,而是启动主应用程序的活动本身。

如果我执行以下操作:

Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setData(Uri.parse("sms:"));
context.startActivity(sendIntent);

然后启动它,而不是我希望它启动应用程序的主要部分,而不是这个新的消息屏幕:

enter image description here

4 个答案:

答案 0 :(得分:15)

  String defaultApplication = Settings.Secure.getString(getContentResolver(), "sms_default_application");
        PackageManager pm = getPackageManager();
        Intent intent = pm.getLaunchIntentForPackage(defaultApplication );
        if (intent != null) {
            startActivity(intent);
        }

答案 1 :(得分:4)

    Intent intent = new Intent(Intent.ACTION_MAIN);

    intent.addCategory(Intent.CATEGORY_DEFAULT);

    intent.setType("vnd.android-dir/mms-sms");

    startActivity(intent);

这可能是你想要的。

答案 2 :(得分:1)

经过长时间的搜索,这就是我最终使用的内容:

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();
}

它支持版本KitKat及更高版本。在此之下,它仅支持接受vnd.android-dir/mms-sms意图的应用。如果无法打开应用程序,应通知用户。

答案 3 :(得分:0)

尝试一下:

String number = "12346556";  // The number on which you want to send SMS  
startActivity(new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", number, null)));  
相关问题