无法在Android Studio中更改全局变量

时间:2017-06-25 07:49:50

标签: java android

我是android新手。我正在尝试获得存储许可。但无法更改onRequestPermissionsResult方法中的全局布尔变量。我不能使permissionIsGranted = true。那么,有没有人可以帮助我。我该怎么办?

package com.plutianclub.musicappv1;

import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;

import java.io.File;
import java.util.ArrayList;
import java.util.jar.Manifest;

public class MainActivity extends AppCompatActivity {

    private static final int MY_PERMISSION_REQUEST_STORAGE_READ = 101;
    private boolean permissionIsGranted = false;

    ListView lv;
    String items;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lv = (ListView) findViewById(R.id.lvPlaylist);


        requestStoragePermission();
        Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_SHORT).show();

            if (permissionIsGranted) {
                Toast.makeText(getApplicationContext(), "wow", Toast.LENGTH_SHORT).show();
                ArrayList<File> mySongs = findSongs(Environment.getExternalStorageDirectory());

                for (int i = 0; i < mySongs.size(); i++) {
                    toast(mySongs.get(i).getName());

                }
            }


    }




    private void requestStoragePermission() {
        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                requestPermissions(new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSION_REQUEST_STORAGE_READ);
            }

        }

    }


    public ArrayList<File> findSongs (File root) {
        ArrayList<File> al = new ArrayList<File>();

        File[] files = root.listFiles();
        for (File singleFile : files){
            if (singleFile.isDirectory() && !singleFile.isHidden()) {
                al.addAll(findSongs(singleFile));
            }else {
                if (singleFile.getName().endsWith(".mp3")){
                    al.add(singleFile);
                }
            }
        }
        return al;

    }

    public void toast(String text){
        Toast.makeText(getApplicationContext(),text,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode){
            case MY_PERMISSION_REQUEST_STORAGE_READ:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    //permission granted
                    permissionIsGranted = true;
                    Toast.makeText(getApplicationContext(), "Permission Granted", Toast.LENGTH_SHORT).show();
                }else {
                    //permission denied
                    permissionIsGranted = false;
                    Toast.makeText(getApplicationContext(), "This application needs Storage permission", Toast.LENGTH_SHORT).show();
                }

                break;

        }
    }





}

1 个答案:

答案 0 :(得分:0)

您需要将上下文传递给requestPermissions方法。然后只有你的 将调用onRequestPermissionsResult方法,您将能够设置权限。

requestPermissions(this, 
               new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSION_REQUEST_STORAGE_READ);