Return 值应该不同于实际值

Return Value should be something else than it is

好的,我的项目应该检查应用程序是否已正确安装。它会检查语言、版本、应用程序是否已收到所需的所有权限等等。目前我在项目的许可部分遇到困难。

这个想法是正确的(或者至少另一个人是这么说的)但是我用“/This is the problem line”标记的行没有 return 正确的值。我将它连接到一个应用程序和所有这些东西,但它只 returned -1 即使权限确实存在于包中并且已启用。

还有部分凭证或至少是名称和数组以及所有非英语的内容,如果需要我可以翻译。如果能提供一些帮助,我将不胜感激!

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
}

我也试过模拟,但我也遇到了同样的问题。但后来我尝试将权限修改为“android.permission.CAMERA”,然后它起作用了。 我在下面附上我的代码供您参考

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