短信不会发送到棉花糖

时间:2017-05-04 12:20:29

标签: android android-6.0-marshmallow

我正在尝试在单击按钮时打开系统默认消息传递活动。这是我的代码:

smsUri = Uri.parse("tel:" + teacherPhone2);
        Intent intent = new Intent(Intent.ACTION_VIEW, smsUri);
        intent.putExtra("address", teacherPhone);
        intent.setType("vnd.android-dir/mms-sms");
        startActivity(intent);

它在MarshMallow下工作正常。但是在MarshMallow中,单击该按钮时应用程序崩溃了。我已设置用户权限并请求发送短信权限。 谁能告诉我为什么会这样呢?

4 个答案:

答案 0 :(得分:0)

在发送短信之前检查运行时权限:

if (Build.VERSION.SDK_INT >= 23) {
            int checkCallPhonePermission = ContextCompat.checkSelfPermission(RegistrationActivity.this,Manifest.permission.SEND_SMS);
            if(checkCallPhonePermission != PackageManager.PERMISSION_GRANTED){
                ActivityCompat.requestPermissions(RegistrationActivity.this,new String[]{Manifest.permission.SEND_SMS},SEND_SMS);
                return;
            }else{
                sendSms(mobile);
            }
        } else {
            sendSms(mobile);
        }

答案 1 :(得分:0)

String[] PERMISSIONS = {Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_SMS, Manifest.permission.CAMERA};

   if(!hasPermissions(this, PERMISSIONS)){
        ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
    }



public static boolean hasPermissions(Context context, String... permissions) {
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
        for (String permission : permissions) {
            if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
    }
    return true;
}

试试这段代码

答案 2 :(得分:0)

1。在清单

中添加SEND_SMS权限
<uses-permission android:name="android.permission.SEND_SMS"/>

2. 如果未获得授权请求权限,否则发送短信

if (Build.VERSION.SDK_INT >= 23) {
    int checkCallPhonePermission = ContextCompat.checkSelfPermission(RegistrationActivity.this,Manifest.permission.SEND_SMS);
    if(checkCallPhonePermission != PackageManager.PERMISSION_GRANTED){
        ActivityCompat.requestPermissions(RegistrationActivity.this,new String[]{Manifest.permission.SEND_SMS},SEND_SMS);
        return;
    } else{
            sendSms(mobile);
    }
} else {
    sendSms(mobile);
}

3。覆盖onActivityResult,以便在授予权限后发送短信

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    int checkCallPhonePermission = ContextCompat.checkSelfPermission(RegistrationActivity.this,Manifest.permission.SEND_SMS);
    if(checkCallPhonePermission == PackageManager.PERMISSION_GRANTED){
        sendSms(mobile);
    }
}

答案 3 :(得分:0)

原因可能是MarshMallow 我们可以关闭特定应用的权限。因此,请在发送短信之前检查权限是否已开启或关闭。