【问题标题】:App closed when it asks permission to user for read write应用程序在向用户请求读写权限时关闭
【发布时间】:2020-06-14 00:16:49
【问题描述】:

我在我的应用程序中遇到问题,我的应用程序需要读写权限,但是每当我的应用程序第一次打开时,我的应用程序崩溃并关闭,然后它会在后台请求权限,然后我需要重新启动它,然后它才能正常工作。我只在第一次运行时遇到问题。

这是我的代码

主要活动

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
        checkPermissions();
    }


    @RequiresApi(api = Build.VERSION_CODES.M)
private void checkPermissions(){

    if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (shouldShowRequestPermissionRationale(
                Manifest.permission.READ_EXTERNAL_STORAGE)) {
            // Explain to the user why we need to read the contacts
        }

        requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                1052);

        // MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE is an
        // app-defined int constant that should be quite unique

        return;
    }
     /*   if (ContextCompat.checkSelfPermission(this,
                android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != android.content.pm.PackageManager.PERMISSION_GRANTED||
                ContextCompat.checkSelfPermission(this,
                        Manifest.permission.READ_EXTERNAL_STORAGE)
                        != android.content.pm.PackageManager.PERMISSION_GRANTED)
            ActivityCompat.requestPermissions(this,
                    new String[]{
                            Manifest.permission.WRITE_EXTERNAL_STORAGE,
                            android.Manifest.permission.READ_EXTERNAL_STORAGE,
                    },
                    1052);*/



}
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {

    switch (requestCode) {
        case 1052: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == android.content.pm.PackageManager.PERMISSION_GRANTED
                    && grantResults[1] == android.content.pm.PackageManager.PERMISSION_GRANTED)
            {
                Toast.makeText(this, "Enjoy This app", Toast.LENGTH_SHORT).show();
            }
            else {

                android.widget.Toast.makeText(this, "Permission Denied, without your permission we cant share or download images to your device", android.widget.Toast.LENGTH_LONG).show();
                // Permission denied - Show a message to inform the user that this app only works
                // with these permissions granted
            }
        }

    }
}

【问题讨论】:

    标签: java android-studio android-permissions


    【解决方案1】:

    您需要通过启动一个新活动并在该新活动中编写上述所有代码来请求许可。我在一个新的android项目中复制了这个问题并验证了这一点。

    您需要做的是创建另一个活动,一个专门为请求权限而创建的活动。让它成为 Main2Activity.java。您的 MainActivity.java 将是

    public class MainActivity extends AppCompatActivity {
    
    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            startActivity(new Intent(MainActivity.this, Main2Activity.class));
        }
    }
    }
    

    那么你的 Main2Activity.java 将是

    public class Main2Activity extends AppCompatActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            checkPermissions();
        }
    }
    @RequiresApi(api = Build.VERSION_CODES.M)
    private void checkPermissions(){
    
        if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
    
            // Should we show an explanation?
            if (shouldShowRequestPermissionRationale(
                    Manifest.permission.READ_EXTERNAL_STORAGE)) {
                // Explain to the user why we need to read the contacts
            }
    
            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                    1052);
    
            // MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE is an
            // app-defined int constant that should be quite unique
    
            return;
        }
     /*   if (ContextCompat.checkSelfPermission(this,
                android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != android.content.pm.PackageManager.PERMISSION_GRANTED||
                ContextCompat.checkSelfPermission(this,
                        Manifest.permission.READ_EXTERNAL_STORAGE)
                        != android.content.pm.PackageManager.PERMISSION_GRANTED)
            ActivityCompat.requestPermissions(this,
                    new String[]{
                            Manifest.permission.WRITE_EXTERNAL_STORAGE,
                            android.Manifest.permission.READ_EXTERNAL_STORAGE,
                    },
                    1052);*/
    
    
    
    }
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
    
        switch (requestCode) {
            case 1052: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && (grantResults[0] == android.content.pm.PackageManager.PERMISSION_GRANTED
                        || grantResults[1] == android.content.pm.PackageManager.PERMISSION_GRANTED))
                {
                    Toast.makeText(this, "Enjoy This app", Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(Main2Activity.this, MainActivity.class));
                    finish();
                }
                else {
    
                    android.widget.Toast.makeText(this, "Permission Denied, without your permission we cant share or download images to your device", android.widget.Toast.LENGTH_LONG).show();
                    // Permission denied - Show a message to inform the user that this app only works
                    // with these permissions granted
                }
            }
    
        }
    }
    }
    

    请注意,在 Main2Activity.java 中,我进行了编辑

    if (grantResults.length > 0
                        && (grantResults[0] == android.content.pm.PackageManager.PERMISSION_GRANTED
                        || grantResults[1] == android.content.pm.PackageManager.PERMISSION_GRANTED))
                {
                    Toast.makeText(this, "Enjoy This app", Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(Main2Activity.this, MainActivity.class));
                    finish();
                }
    
    1. 要让 Toast 出现,android.content.pm.PackageManager.PERMISSION_GRANTED 将在 grantResults[0] 或 grantResults[1] 中,但不能同时在两者中。
    2. 我使用了函数 startActivity() 和 finish(),因为在获得权限后,这个专门为权限创建的活动将关闭并打开 MainActivity.java。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-05
    • 2021-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多