Android KitKat 4.4 Hangouts 无法处理发送短信意图 [英] Android KitKat 4.4 Hangouts cannot handle Sending SMS intent

查看:33
本文介绍了Android KitKat 4.4 Hangouts 无法处理发送短信意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

发送短信的代码在 Android 4.3 (Jelly Bean) 之前一直运行良好,但从 4.4 (KitKat) 开始就停止工作了

Code for sending sms that worked perfectly until Android 4.3 (Jelly Bean) and stopped working since 4.4 (KitKat)

我只是在为用户准备短信,但他们需要选择要发送到的号码.

I'm just preparing the text message for the user, but they need to choose the number to send to.

我使用的代码是:

Intent sendIntent = new Intent(Intent.ACTION_VIEW);         
    sendIntent.setData(Uri.parse("sms:"));
    sendIntent.putExtra("sms_body", smsText);

    activity.startActivity(sendIntent);

由于它停止工作,我也尝试了 ACTION_SENDACTION_SENDTO 都没有奏效,我也尝试了 sendIntent.setType("vnd.android-dir/mms-sms");,但同样没有任何效果.

Since it stopped working, I have also tried the ACTION_SEND and ACTION_SENDTO Neither worked, I also tried the sendIntent.setType("vnd.android-dir/mms-sms");, but again nothing worked.

我查看了 Stack Overflow 上的几个答案 答案 1答案 2,但两个答案没有处理我的要求.

I looked at several answers on Stack Overflow answer 1 and answer 2, but both answers aren't dealing with the requirements I have.

我想做什么:

  • 仅使用短信应用发送短信,而不是所有服务于发送意图的应用
  • 为用户准备文本
  • 让用户选择要向其发送消息的电话号码

对于版主:这不是重复的问题,因为这些问题不会问完全相同的问题,这里需要的是发送没有电话号码的短信,并且没有处理任何问题和答案.

For moderators: It is not a duplicate questions, since the questions, doesn't ask the exact same thing, the need here is to send sms with no phone number, and none of the questions and answers dealt with that.

推荐答案

我附上了通过执行以下操作来解决问题的代码:

I attached a code that solve the problem by doing the following:

  • 检查操作系统版本
  • 如果是旧版本(KitKat 之前),请使用旧方法
  • 如果是新 API,请检查默认的 sms 包.有的话,设置为包,否则,让用户选择分享应用.

代码如下:

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

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

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

    }
    else //For early versions, do what worked for you before.
    {
        Intent sendIntent = new Intent(Intent.ACTION_VIEW);
        sendIntent.setData(Uri.parse("sms:"));
        sendIntent.putExtra("sms_body", smsText);
        activity.startActivity(sendIntent);
    }

这篇关于Android KitKat 4.4 Hangouts 无法处理发送短信意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆