Supagarn
Supagarn

Reputation: 45

StartActivity call requires permissions problems

I follow this video tutorial Previously, It has no problems .

Today I try again , but it has some problem with it.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>

Upvotes: 3

Views: 3011

Answers (3)

Shaishav Jogani
Shaishav Jogani

Reputation: 2121

After API 23, User has to explicitly give approval to any permission that may access user's confidential data. Such permissions contains Calendar, Camera, Contact, Location, Microphone, Phone, Sensor, SMS, Storage.

Below is the code I taken from Documentation with some modification.

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);
    }
}

And then override below method

@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
    }
}

Upvotes: 0

Vishal Puri
Vishal Puri

Reputation: 843

You should use

Intent.ACTION_DIAL 

instead of Intent.ACTION_CALL. Intent.ACTION_DIAL which prepopulates the dialer with the number you pass it. It doesn't require permission.

Hope it solves the problem!

Upvotes: 1

CJR
CJR

Reputation: 3572

You need to add (runtime) permissions check. Starting from Android API 23 and higher, you need to ask for permissions to do certain tasks(Ex: calls, location info...) during runtime. To do so, simply, click on this code:

startActivity(callIntent);

Go to the red warning triangle/sign left to your code. Click on it, and click on add permission check. Now when you run your app, a "dialog" will pop up, and ask the user to allow the app to make calls.

Hope this helps. For more info see this links:

Android permission checks

or check out this previously answered question (as suggested by @CommonsWare):

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

Upvotes: 1

Related Questions