我尝试发送短信时应用崩溃

时间:2017-09-12 12:39:42

标签: android sms kotlin

我尝试在kotlin发送短信,我用以下代码编写简单的应用程序:

Panels

当我尝试通过USB电缆从AndroidStudio应用程序运行应用程序时,不希望权限,尽管我已经添加了SEND_SMS权限以显示。当我按下按钮关闭

2 个答案:

答案 0 :(得分:0)

您必须自己申请许可。 如此guide

中所述
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
            Manifest.permission.SEND_SMS)
    != PackageManager.PERMISSION_GRANTED) {

    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
        Manifest.permission.SEND_SMS)) {

        // Show an explanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.

    } else {

        // No explanation needed, we can request the permission.

        ActivityCompat.requestPermissions(thisActivity,
            new String[]{Manifest.permission.SEND_SMS},
            MY_PERMISSIONS_REQUEST_SEND_SMS);

        // MY_PERMISSIONS_REQUEST_SEND_SMS is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
}

答案 1 :(得分:-1)

如果您不想使用运行时权限,请在Gradle中将目标SDK版本设置为21.

Gradle 中添加目标SDK ,如下所示:

android {
    defaultConfig {
        .....
        .....
        targetSdkVersion 21
    }
}