Return Value should be something else than it is Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Return Value should be something else than it is without wasting too much if your time.

The question is published on by Tutorial Guruji team.

Okay so I project which is supposed to check if an app has been installed correctly. It checks the language, the version, if the app has received all the permission it needs and more. At the moment I am having difficulties with the permission part of the project.

The idea is correct (or at least so said another person) but the line which I marked with “/This is the problem line” does not return the right value. I had it connected to an app and all that stuff but it only returned -1 even though the permission did exist in the package and was enabled.

Also part of the scrip or at least the names and arrays and all that are not in English, if needed I can translate it. I would be really grateful for some help!

public void berechtigungen(View view){


    String[] berechtigungAbfrage = {
            //A list of all the permissions
            "Manifest.permission.ACCESS_FINE_LOCATION",
            "Manifest.permission.ACCESS_COARSE_LOCATION",
            "Manifest.permission.CALL_PHONE",
            "Manifest.permission.CAMERA",
            "Manifest.permission.PROCESS_OUTGOING_CALLS",
            "Manifest.permission.READ_CALENDAR",
            "Manifest.permission.READ_CALL_LOG",
            "Manifest.permission.READ_CONTACTS",
            "Manifest.permission.READ_EXTERNAL_STORAGE",
            "Manifest.permission.READ_PHONE_STATE",
            "Manifest.permission.READ_SMS",
            "Manifest.permission.RECEIVE_MMS",
            "Manifest.permission.RECEIVE_SMS",
            "Manifest.permission.RECEIVE_WAP_PUSH",
            "Manifest.permission.RECORD_AUDIO",
            "Manifest.permission.SEND_SMS",
            "Manifest.permission.USE_SIP",
            "Manifest.permission.WRITE_CALENDAR",
            "Manifest.permission.WRITE_CALL_LOG",
            "Manifest.permission.WRITE_CONTACTS",
            "Manifest.permission.WRITE_EXTERNAL_STORAGE",
    };

    String[] berechtigungCheck = {
            //A list of all permissions which will get displayed in the target language
            "Zugriff auf genaue Position",
            "Zugriff auf grobe Position",
            "Telefon",
            "Kamera",
            "Ausgehende Anrufe verarbeiten",
            "Kalender lesen",
            "Anrufliste lesen",
            "Kontakte lesen",
            "Externen Speicher lesen",
            "Telefonstatus lesen",
            "SMS lesen",
            "MMS empfangen",
            "SMS empfangen",
            "Wap Push erhalten",
            "Ton aufnehmen",
            "SMS senden",
            "Benutze einen Schluck",
            "Kalender bearbeiten",
            "Anrufliste bearbeiten",
            "Kontakte bearbeiten",
            "Externen Speicher bearbeiten",
    };

    for(int i = 0; i < 21; i++) {
        PackageManager pm = getPackageManager();
        int permissions = pm.checkPermission(berechtigungAbfrage[i],"I can't give the actuall package name but this should work for any app"); //this is is the problem line
        //checks for the Permission i in the package
            if (permissions == 0) {
                //0 permission exists in this package
                //-1 permission doesnt exist in this package
                int antwort = checkSelfPermission(berechtigungAbfrage[i]); //checks
                    if (antwort == 0) {
                        //0 : the user has given permission
                        //-1 : the user hasn't given the permission
                        berechtigungCheck[i] = berechtigungCheck[i] + "Vorhanden";
                    } else {
                        berechtigungCheck[i] = berechtigungCheck[i] + "Nicht Vorhanden";
                    }
            }
        else{
            berechtigungCheck[i] = berechtigungCheck[i] + " Nicht gebraucht";
                //If the permission does not exist in the package the system simply says "is not needed"
        }
    }
    new AlertDialog.Builder(this).setItems(berechtigungCheck,null).setTitle("Berechtigung").setPositiveButton("ok",null).show();
    //All of this gets shown when a button is clicked
}

Answer

I tried to simulate the same but I too faced the same problem. But then I tried modifying the permissions as “android.permission.CAMERA” and then it worked. I am attaching my code below for your reference

String[] permissions = {"android.permission.ACCESS_FINE_LOCATION",
                        "android.permission.ACCESS_COARSE_LOCATION",
                        "android.permission.CALL_PHONE",
                        "android.permission.PROCESS_OUTGOING_CALLS",
                        "android.permission.READ_CALENDAR",
                        "android.permission.CAMERA"};
for(int iter = 0;iter < permissions.length;iter++){
        PackageManager packageManager = getPackageManager();
        int permInt = packageManager.checkPermission(permissions[iter],getApplicationContext().getPackageName());
        ...
    }
We are here to answer your question about Return Value should be something else than it is - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji