Android 通过意向发送短信

Android 通过意向发送短信,android,android-intent,sms,Android,Android Intent,Sms,我想通过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", p

我想通过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);
为什么?

此外,我知道一种跟踪短信发送的方法,但我不知道该如何编码:

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

其中XXXXXXXXXX是电话号码。

请尝试此代码。它会起作用的

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

希望这将对您有所帮助。

我从一个博客开发了此功能。有两种方式可以发送短信

  • 开放本机SMS编写器
  • 编写消息并从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>
    
    注意:- 在这个方法中,您不需要在AndroidManifest.xml文件中拥有SEND_SMS权限

    关于第二种方法,请参考此。你会从这里找到一个很好的解释


    希望这能帮助你…

    希望这是有效的,这在我的应用程序中有效

    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);  
    
    SmsManager.getDefault().sendTextMessage("Phone Number", null, "Message", null, null);
    

    创建如下意图:

    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");
    smsIntent.setFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(smsIntent);
    

    这是使用SMSManager的另一个解决方案:

    SmsManager smsManager = SmsManager.getDefault();
    smsManager.sendTextMessage("PhoneNumber-example:+989147375410", null, "SMS Message Body", null, null);
    

    创建如下意图:

       Uri uriSms = Uri.parse("smsto:1234567899");   
       Intent intentSMS = new Intent(Intent.ACTION_SENDTO, uriSms);   
       intentSMS.putExtra("sms_body", "The SMS text");   
       startActivity(intentSMS); 
    

    如果您想要某条消息,请使用以下命令:

    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);
    
  • 清单权限(您可以将其放在“应用程序”之后或之前)
  • 例如,制作一个按钮,并编写以下代码(如前所述,由
    Prem
    在该线程中编写),然后将以下电话号码替换为实际号码,即可工作:

  • 添加try-catch,否则没有sim卡的手机将崩溃

    void sentMessage(String msg) {
        try {
            Intent smsIntent = new Intent(Intent.ACTION_VIEW);
            smsIntent.setType("vnd.android-dir/mms-sms");
            smsIntent.putExtra("sms_body", msg);
            startActivity(smsIntent);
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, "No SIM Found", Toast.LENGTH_LONG).show();
        }
    }
    


    请尝试阅读android源代码,也可以是smsmsmanager。您还可以获得
    ActivityNotFoundException:No Activity found to handle Intent(“vnd.android dir/mms sms”)
    。最好不要使用这种方法。还有一件事,为了测试这个应用程序,你可以打开2个模拟器;他们有5554和5555之类的证件。您可以使用它作为号码并对其进行测试。这是从所有设备和android版本通过intent发送短信的唯一有效方式。@I-droid:您指的是哪种方法?@MarcoAltran:
    sendSMS(View v)
    @Prem最好包含源代码中的内容,然后添加对它的引用。链接迟早会被破坏,然后突然间你的答案将不包含足够的信息。这在Android 4.0.3(更好?)上不起作用。我必须使用Intent.EXTRA_文本。你知道为什么吗?刚刚在两台运行安卓6.0.1和4.4.4的设备上进行了测试:在所有情况下,
    “sms_body”
    工作正常,
    Intent.EXTRA_TEXT
    不起作用。这种方式需要特殊权限吗?来自文档:使用此方法需要您的应用程序具有发送短信权限。这也仅在消息长度小于等于160个字符时有效。如果超过160,您必须使用
    sendMultipartTextMessage
    而不是
    sendTextMessage
    抱歉向下投票,此方法需要发送短信权限,并且不会被接受,谷歌将不允许发布此应用,除非它是核心短信应用。这意味着您的应用程序的主要功能必须是SMS messenger,很少有例外。然而,大多数应用程序将不会被批准,并将在推出期间被拒绝。是的!谢谢:)它帮助了我。谢谢。这就是我想要的小心,现在是2021年,这已经不起作用了。。它不会在“打开方式”选项中显示默认SMS提供商。特别是google默认的“Messages”应用程序没有出现在selectionWhere is
    中。PutExtra
    来自哪里?似乎有什么地方出错了,我收到警告,设置类型将覆盖设置URI2021,这不再起作用。。它不会在“打开方式”选项中显示默认SMS提供商。特别是,谷歌默认的“消息”应用程序不会出现在选择中。除非它是核心的SMS messenger应用程序,否则此解决方案将不再被接受。我们不需要SEND_SMS权限来使用intent发送SMS。
    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);
    
     uses-permission android:name="android.permission.SEND_SMS"/>
    
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", "phone_Number", null)));
    
    void sentMessage(String msg) {
        try {
            Intent smsIntent = new Intent(Intent.ACTION_VIEW);
            smsIntent.setType("vnd.android-dir/mms-sms");
            smsIntent.putExtra("sms_body", msg);
            startActivity(smsIntent);
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, "No SIM Found", Toast.LENGTH_LONG).show();
        }
    }