SecurityException android [duplicate]

0

When I run the following code in Android Studio:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    LocationManager lcmanager = (LocationManager) getSystemService(LOCATION_SERVICE);
    List<String> listadeprovider = lcmanager.getAllProviders();
    LocationProvider locationP;
    for(int i = 0; i < listadeprovider.size(); i++)
    {
        locationP = lcmanager.getProvider(listadeprovider.get(i));
        System.out.println("\nPrecisao: " + locationP.getAccuracy() + "\nSuporta Altitude: " + locationP.supportsAltitude() + "\nRecursos: " + locationP.getPowerRequirement() + "\n");
    }

    }

}

and I get the following error message stating that I have not added the ACCESS_FINE_LOCATION permission:

FATAL EXCEPTION: main
Process: com.example.sullyvan.k19treinamentos, PID: 16601
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sullyvan.k19treinamentos/com.example.sullyvan.k19treinamentos.MainActivity}: java.lang.SecurityException: "passive" location provider requires ACCESS_FINE_LOCATION permission.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.SecurityException: "passive" location provider requires ACCESS_FINE_LOCATION permission.
at android.os.Parcel.readException(Parcel.java:1599)
at android.os.Parcel.readException(Parcel.java:1552)
at android.location.ILocationManager$Stub$Proxy.getProviderProperties(ILocationManager.java:1031)
at android.location.LocationManager.getProvider(LocationManager.java:373)
at com.example.sullyvan.k19treinamentos.MainActivity.onCreate(MainActivity.java:26)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 

I already added this permission in Android Manifest

<?xml version="1.0" encoding="utf-8"?>

<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"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".SecondActivity"/>
</application>

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

Can anyone help me with the question?

    
asked by anonymous 03.06.2016 / 16:37

2 answers

2

Permissions management has changed. It is now necessary to request permission from the user when it is required. No more by Android manifest (for new versions). For the old versions, leave the manifest. So, I'll give an example here of how I cite certain permissions in my app.

private Boolean checkHasPermissions(){

        Boolean permissionsOk = false;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            List<String> permissoes = new ArrayList<>();

            if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                permissoes.add(android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
            }

            if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                permissoes.add(android.Manifest.permission.CAMERA);
            }

            if(permissoes.size() > 0) {
                ActivityCompat.requestPermissions(this, permissoes.toArray(new String[permissoes.size()]), PERMISSIONS_REQUEST);
            }else{
                permissionsOk = true;
            }
        }else{
            permissionsOk = true;
        }

        return permissionsOk;
    }

Check the request for the static requestPermissions method. The return you can handle overriding the onRequestPermissionResult method.

@Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case PERMISSIONS_REQUEST:
                // se a requisição for cancelada com "back" tratar como não concedida.
                if (grantResults.length > 0) {

                    Boolean allPermissionsAccepted = true;
                    //verificar se tem alguma permissão não concedida
                    for (int i = 0; i < grantResults.length; i++) {
                        if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
                            allPermissionsAccepted = false;
                            break;
                        }
                    }

                    if (allPermissionsAccepted) {
                        setResultOkAndClose();
                    } else {
                        startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), RESULT_SETTINGS);
                    }
                }else{
                    startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), RESULT_SETTINGS);
                }
                break;
        }
    }
    
03.06.2016 / 17:53
-1

Try add android:exported="true" in your manifest file in the activity you are trying to run

    
03.06.2016 / 16:43