【问题标题】:Sending SMS with Intent not working in Oreo 8.1 Google API emulator在 Oreo 8.1 Google API 模拟器中使用 Intent 发送短信不起作用
【发布时间】:2018-04-23 16:29:41
【问题描述】:

我正在使用此代码发送短信:

此代码可以在许多教程中找到,但它在 Oreo 中不起作用,我已发送正确答案

public void sendSms(String phone) {
        if(null != phone) {
            final Intent i = new Intent(Intent.ACTION_VIEW);
            i.setType("vnd.android-dir/mms-sms");//<-- maybe problem is here
            i.putExtra("address", phone);
            startActivity(Intent.createChooser(i, getString(R.string.sms)));
        }
    }

我已经在 Android 4 到 Android 6 中测试了此代码没有问题,但在 Android 8.1 中,google api 模拟器说没有应用程序可以执行此操作,但是这个模拟器已经安装了 SMS 应用程序

我也不知道这是否适用于使用 Oreo 8.1 的真实设备

【问题讨论】:

    标签: android android-intent sms


    【解决方案1】:

    如果您希望用户无需在此部分输入电话号码即可发送短信 Uri.parse("smsto: " + phone) 并让他在去奥利奥发短信之前选择联系人,奥利奥及更低版本可以使用此代码:

    Intent sendIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("sms:"));
    sendIntent.putExtra("sms_body", "your message");
    
    if(android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
           sendIntent.setType("vnd.android-dir/mms-sms");           
    
    context.startActivity(sendIntent);
    

    【讨论】:

    • ifelse 之间的唯一区别是 else 子句中的 setType。您是否测试较低奥利奥版本的 if 子句也许它也适用于较低版本。
    • @WIZARD 你是对的,它适用于所有版本.. 我编辑了答案,谢谢
    • @WIZARD ,在较低版本中它显示 apphChooser 我想直接发送短信
    • 一个快速的答案是将这些行 if(android.os.Build.VERSION.SDK_INT &lt; Build.VERSION_CODES.O) smsIntent.setType("vnd.android-dir/mms-sms"); 添加到您的答案中
    【解决方案2】:

    终于我自己找到了答案:

    public void sendSms(String phone, String sms) {
            if(null != phone) {
                Intent i = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto: " + phone));
                i.putExtra("sms_body", sms);
                if (i.resolveActivity(getPackageManager()) != null) {
                    startActivity(i);
                }else{
                    Toast.makeText(this, "SMS App not found", Toast.LENGTH_LONG).show(); 
                }
             }
    }
    

    【讨论】:

    • 有没有办法在不设置电话号码的情况下进入消息应用程序?
    • 它对我不起作用,但我找到了另一个解决方案,我现在就发布它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-22
    • 2017-03-05
    • 2016-02-12
    • 2018-05-20
    • 1970-01-01
    相关资源
    最近更新 更多