StartActivity调用需要权限问题

时间:2016-09-10 13:22:17

标签: android android-intent phone-call

I follow this video tutorial以前,没有问题。

今天我再试一次,但它有一些问题。This picture

select t1.CODE
from table1 t1
join (select top 1 IDK from table2 order by QTY desc) xxx
on t1.IDK = xxx.IDK
where t1.Name = 'PEN';

3 个答案:

答案 0 :(得分:1)

您需要添加(运行时)权限检查。从Android API 23及更高版本开始,您需要在运行时期间要求权限执行某些任务(例如:调用,位置信息...)。为此,只需单击此代码:

startActivity(callIntent);

转到代码左侧的红色警告三角形/符号。单击它,然后单击添加权限检查。现在当你运行你的应用程序时,一个"对话框"将弹出,并要求用户允许该应用拨打电话。

希望这会有所帮助。有关详细信息,请参阅此链接:

Android permission checks

或查看此前已回答的问题(由@CommonsWare建议):

Android permission doesn't work even if I have declared it

答案 1 :(得分:1)

你应该使用

Intent.ACTION_DIAL 

而不是 Intent.ACTION_CALL 。 Intent.ACTION_DIAL,它使用您传递的号码预先填充拨号器。它不需要许可。

希望它能解决问题!

答案 2 :(得分:0)

在API 23之后,用户必须明确批准可能访问用户机密数据的任何权限。此类权限包含Calendar, Camera, Contact, Location, Microphone, Phone, Sensor, SMS, Storage.

以下是我从Documentation获取的代码,但有一些修改。

private void callNumber() {

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE)
            != PackageManager.PERMISSION_GRANTED) {
        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.CALL_PHONE)) {
              // Show an expanation 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(this,
                    new String[]{Manifest.permission.CALL_PHONE},
                    MY_PERMISSIONS_REQUEST_READ_CONTACTS);

        }
    } else {
        //This is for lower version <23
        Intent callIntent = new Intent(Intent.ACTION_DIAL);
        callIntent.setData(Uri.parse("tel:" + MyCellNumber);
        startActivity(callIntent);
    }
}

然后覆盖以下方法

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                Intent callIntent = new Intent(Intent.ACTION_DIAL);
                callIntent.setData(Uri.parse("tel:" + MyCellNumber));
                startActivity(callIntent);
                // permission was granted, yay! Do the
                // contacts-related task you need to do.
            } else {
                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}