Android 有意图地启动短信应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2372248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:34:26  来源:igfitidea点击:

launch sms application with an intent

androidsmsandroid-intent

提问by Olivier69

I have a question about an intent... I try to launch the sms app...

我有一个关于意图的问题......我尝试启动短信应用......

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setType("vnd.android-dir/mms-sms");
int flags = Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP |
    Intent.FLAG_ACTIVITY_CLEAR_TOP;
intent.setFlags(flags);
intent.setData(Uri.parse("content://sms/inbox"));
context.startActivity(intent);

so, you can see that I put too much things in my intent, but that's because I don't know how I can do... Thank's

所以,你可以看到我在我的意图中放了太多东西,但那是因为我不知道我该怎么做......谢谢

回答by jqpubliq

To start launch the sms activity all you need is this:

要开始启动短信活动,您只需要:

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

You can add extras to populate your own message and such like this

您可以添加额外内容来填充您自己的消息等

sendIntent.putExtra("sms_body", x); 

then just startActivity with the intent.

然后就开始有意图的活动。

startActivity(sendIntent);

回答by Pratik Sharma

Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address", "12125551212");
smsIntent.putExtra("sms_body","Body of Message");
startActivity(smsIntent);

回答by android_developer

If android version is Kitkat or above, users can change default sms application. This method will get default sms app and start default sms app.

如果 android 版本是 Kitkat 或更高版本,用户可以更改默认的短信应用程序。此方法将获取默认短信应用程序并启动默认短信应用程序。

private void sendSMS() {    
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) // At least KitKat
      {
         String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(this); // Need to change the build to API 19

         Intent sendIntent = new Intent(Intent.ACTION_SEND);
         sendIntent.setType("text/plain");
         sendIntent.putExtra(Intent.EXTRA_TEXT, "text");

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

      }
      else // For early versions, do what worked for you before.
      {
         Intent smsIntent = new Intent(android.content.Intent.ACTION_VIEW);
         smsIntent.setType("vnd.android-dir/mms-sms");
         smsIntent.putExtra("address","phoneNumber");         
         smsIntent.putExtra("sms_body","message");
         startActivity(smsIntent);
      }
   }

回答by skyisle

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setType("vnd.android-dir/mms-sms");
startActivity(intent);

That's all you need.

这就是你所需要的。

回答by Pir Fahim Shah

If you want to launch SMS Composing activity from some of your other activity and you also have to pass a phone number and SMS text, then use this code:

如果您想从其他一些活动中启动 SMS 编写活动,并且还必须传递电话号码和 SMS 文本,请使用以下代码:

Uri sms_uri = Uri.parse("smsto:+92xxxxxxxx"); 
Intent sms_intent = new Intent(Intent.ACTION_SENDTO, sms_uri); 
sms_intent.putExtra("sms_body", "Good Morning ! how r U ?"); 
startActivity(sms_intent); 

Note:here the sms_bodyand smsto:is keys for recognizing the text and phone no at SMS compose activity, so be careful here.

注意:这里的sms_bodysmsto:是识别短信编写活动中文本和电话号码的关键,所以在这里要小心。

回答by JaydeepW

Here is the code that will open the SMS activity pre-populated with the phone number to which the SMS has to be sent. This works fine on emulator as well as the device.

这是打开 SMS 活动的代码,该活动预先填充了 SMS 必须发送到的电话号码。这适用于模拟器和设备

Intent smsIntent = new Intent(Intent.ACTION_SENDTO);
smsIntent.addCategory(Intent.CATEGORY_DEFAULT);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.setData(Uri.parse("sms:" + phoneNumber); 

回答by Harinder

Use

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setClassName("com.android.mms", "com.android.mms.ui.ConversationList");

回答by Vinayak V Naik

Intent eventIntentMessage =getPackageManager()
 .getLaunchIntentForPackage(Telephony.Sms.getDefaultSmsPackage(getApplicationContext));
startActivity(eventIntentMessage);

回答by BuffMcBigHuge

try {
    Intent smsIntent = new Intent(Intent.ACTION_VIEW);
    smsIntent.setData(Uri.parse("smsto:" + Uri.encode(number)));
    smsIntent.putExtra("address", number);
    smsIntent.putExtra("sms_body", message);

    PackageManager pm = activity.getPackageManager();
    List<ResolveInfo> resInfo = pm.queryIntentActivities(smsIntent, 0);

    for (int i = 0; i < resInfo.size(); i++) {
        ResolveInfo ri = resInfo.get(i);
        String packageName = ri.activityInfo.packageName;

        if (packageName.contains("sms")) {
            //Log.d("TAG", packageName + " : " + ri.activityInfo.name);
            smsIntent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
        }
    }
    activity.startActivity(smsIntent);
} catch (Exception e) {
    // Handle Error
}

Best way of doing this.

这样做的最佳方式。

回答by Delgado

I use:

我用:

Intent sendIntent = new Intent(Intent.ACTION_MAIN);
sendIntent.putExtra("sms_body", "text");
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);