通过意图发送短信

Ata*_*Ata 68 sms android android-intent

我想通过意图发送短信,但是当我使用此代码时,它会将我重定向到错误的联系人:

Intent intentt = new Intent(Intent.ACTION_VIEW);         
intentt.setData(Uri.parse("sms:"));
intentt.setType("vnd.android-dir/mms-sms");
intentt.putExtra(Intent.EXTRA_TEXT, "");
intentt.putExtra("address",  phone number);
context.startActivity(intentt);
Run Code Online (Sandbox Code Playgroud)

为什么?此外,我知道一种方法来跟踪短信发送,但我不知道如何编码:

Starting activity: Intent { 
   act=android.intent.action.SENDTO dat=smsto:%2B**XXXXXXXXXXXX** flg=0x14000000    
   cmp=com.android.mms/.ui.ComposeMessageActivity }
Run Code Online (Sandbox Code Playgroud)

其中XXXXXXXXXXXX是电话号码.

Pre*_*rem 79

我从一个博客开发了这个功能.您可以通过两种方式发送短信.

  1. 打开本机短信作曲家
  2. 编写您的消息并从您的Android应用程序发送

这是第一种方法的代码.

main.xml中

<?xml version="1.0" encoding="utf-8"?>  
    <RelativeLayout  
        android:id="@+id/relativeLayout1"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"  
        xmlns:android="http://schemas.android.com/apk/res/android">  

            <Button  
                android:id="@+id/btnSendSMS"  
               android:layout_height="wrap_content"  
               android:layout_width="wrap_content"  
               android:text="Send SMS"  
               android:layout_centerInParent="true"  
               android:onClick="sendSMS">  
           </Button>  
   </RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

活动

public class SendSMSActivity extends Activity {  
     /** Called when the activity is first created. */  
     @Override  
     public void onCreate(Bundle savedInstanceState) {  
         super.onCreate(savedInstanceState);  
         setContentView(R.layout.main);  
      }  

     public void sendSMS(View v)  
     {  
         String number = "12346556";  // The number on which you want to send SMS  
         startActivity(new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", number, null)));  
     }  
    /* or 
     public void sendSMS(View v) 
      { 
     Uri uri = Uri.parse("smsto:12346556"); 
         Intent it = new Intent(Intent.ACTION_SENDTO, uri); 
         it.putExtra("sms_body", "Here you can set the SMS text to be sent"); 
         startActivity(it); 
      } */  
 }
Run Code Online (Sandbox Code Playgroud)

注意: - 在此方法中,您不需要AndroidManifest.xml文件中的SEND_SMS权限.

对于第二种方法,请参阅此博客.你会从这里找到一个很好的解释.

希望对你有帮助...

  • 还有一件事,为了测试这个应用程序的目的,你可以打开2个模拟器; 他们有像5554和5555或类似的东西.您将此作为数字并进行测试. (3认同)

Bao*_* Le 50

Uri uri = Uri.parse("smsto:YOUR_SMS_NUMBER");   
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);   
intent.putExtra("sms_body", "The SMS text");   
startActivity(intent);  
Run Code Online (Sandbox Code Playgroud)

  • 这在Android 4.0.3上不起作用(还有更好的?).我必须使用Intent.EXTRA_TEXT.知道为什么吗? (2认同)

Sha*_*shi 32

Intent smsIntent = new Intent(android.content.Intent.ACTION_VIEW);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address","your desired phoneNumber");         
smsIntent.putExtra("sms_body","your desired message");
startActivity(smsIntent);
Run Code Online (Sandbox Code Playgroud)

希望这对某人有所帮助

  • 小心,现在是 2021 年了,这不再起作用了..它不会在“打开方式”选择中显示默认短信提供商。特别是谷歌的默认“消息”应用程序不会出现在选择中 (2认同)

Vip*_*hah 5

试试这个代码.它会工作

Uri smsUri = Uri.parse("tel:123456");
Intent intent = new Intent(Intent.ACTION_VIEW, smsUri);
intent.putExtra("sms_body", "sms text");
intent.setType("vnd.android-dir/mms-sms"); 
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

希望这会帮助你.


the*_*n05 5

如果您想要特定的消息,请使用以下命令:

String phoneNo = "";//The phone number you want to text
String sms= "";//The message you want to text to the phone

Intent smsIntent = new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", phoneNo, null));
smsIntent.putExtra("sms_body",sms);
startActivity(smsIntent);
Run Code Online (Sandbox Code Playgroud)