gpt4 book ai didi

java - 应用程序在请求用户读写权限时关闭

转载 作者:行者123 更新时间:2023-12-01 16:17:55 24 4
gpt4 key购买 nike

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

这是我的代码

主要 Activity

        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
}
}

}
}

最佳答案

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

您需要做的是创建另一个 Activity ,一个专门为请求权限而创建的 Activity 。让它成为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() 函数,因为在获得许可后,专门为许可而创建的此 Activity 将关闭并打开 MainActivity.java。

关于java - 应用程序在请求用户读写权限时关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62357470/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com