Securityexception android

Asked

Viewed 771 times

0

When I run in Android Studio the code below:

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 ACCES_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) 

since 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 someone help me with the question?

2 answers

2


Permissions management has changed. It is now necessary to request permission from the user at the time it is needed. No more by Android manifest (for the new versions). For the old versions, leave in the manifest. So, let me give you an example here of how I check 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 call of the static method requestPermissions. The return you can treat by overriding the method onRequestPermissionResult.

@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;
        }
    }
  • It worked perfectly. Thank you.

  • Good! Thanks for the comment!

-1

Try add android:exported="true" in his manifest file in Activity that you’re trying to run

Browser other questions tagged

You are not signed in. Login or sign up in order to post.