StartActivity调用需要权限问题

StartActivity call requires permissions problems

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

今天又试了一次,还是有点问题。This picture

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.fooddeliverycaller">


<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<uses-sdk
    android:minSdkVersion="15"
    android:targetSdkVersion="24" />


<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <meta-data android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version"/>

    <activity android:name="com.google.android.gms.ads.AdActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
</application>

您需要添加(运行次)权限检查。从 Android API 23 岁及以上开始,您需要请求权限才能在 运行 时间内执行某些任务(例如:通话、位置信息...)。为此,只需单击此代码:

startActivity(callIntent);

转到代码左侧的红色警告 triangle/sign。单击它,然后单击添加权限检查。现在,当您 运行 您的应用程序时,会弹出一个 "dialog",并要求用户允许该应用程序拨打电话。

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

Android permission checks

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

你应该使用

Intent.ACTION_DIAL 

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

希望能解决问题![​​=11=]

在API23之后,用户必须明确同意任何可能访问用户机密数据的权限。这样的权限包含 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
    }
}