onRequestPermissionsResult 在 Android 片段中已弃用

onRequestPermissionsResult is deprecated in Android Fragment

这是我当前(已弃用)的方法:

int LOCATION_REQUEST_CODE = 10001;     
@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        if (requestCode == LOCATION_REQUEST_CODE) {
            //Permission granted
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                checkSettingsAndStartLocationUpdates();
            } else {
                //DO SOMETHING - Permission not granted
            }
        }
    }

根据 android 文档 https://developer.android.com/training/basics/intents/result 我应该使用 registerForActivityResult():

// GetContent creates an ActivityResultLauncher<String> to allow you to pass
// in the mime type you'd like to allow the user to select
ActivityResultLauncher<String> mGetContent = registerForActivityResult(new GetContent(),
    new ActivityResultCallback<Uri>() {
        @Override
        public void onActivityResult(Uri uri) {
            // Handle the returned Uri
        }
});

但是我正在努力更换我的方法。 在新方法“registerForActivityResult()”中,我在哪里插入我的请求代码和我的 int 数组?为什么我需要一个“Uri”?

我认为您使用的方法不正确。

你给我的示例代码显示了如何将 image path 作为参数传递,因为在处理 android 中的图像时,图像是本地图像的实际路径(你看到“mime在评论中输入“?)

由于 onRequestPermissionsResult 已弃用,您有两种选择来解决此问题。

  1. 添加 @SuppressWarnings("deprecation") 以忽略弃用警告并仍然使用它

  2. 或使用新权限API。查看代码

     import androidx.activity.result.contract.ActivityResultContracts.*
     import androidx.activity.registerForActivityResult
    
     class SampleActivity : AppCompatActivity() {
         val requestLocation: () -> Unit = registerForActivityResult(
         RequestPermission(), ACCESS_FINE_LOCATION // Permission Type
         ) { isGranted ->
            // to do after permission is granted
         }
    
         private fun requestLocationAction() {
             requestLocation()
         }
     }
    

Where do I insert my requestcode and my int array in the new method "registerForActivityResult()" ?

您没有任何requestCode。在此设计中,您可以为之前使用的每个 requestCode 考虑一个回调。对于处理单个权限,您可以使用内置的 ActivityResultContracts.RequestPermission:

// Register the permissions callback, which handles the user's response to the
// system permissions dialog. Save the return value, an instance of
// ActivityResultLauncher, as an instance variable.
private ActivityResultLauncher<String> requestPermissionLauncher = registerForActivityResult(new RequestPermission(), isGranted -> {
    if (isGranted) {
        // Permission is granted. Continue the action or workflow in your
        // app.
    } else {
        // Explain to the user that the feature is unavailable because the
        // features requires a permission that the user has denied. At the
        // same time, respect the user's decision. Don't link to system
        // settings in an effort to convince the user to change their
        // decision.
    }
});

Why do I need a "Uri" ?

你可能理解错了。这样,您需要一个 ActivityResultContract 来连接两个 Activity。以前您只能将 Intent 传递给起始 Activity。现在您可以使用此合同传递任何类型的对象。 Input 是你要传递的对象类型,Output 是从新 Activity 返回的结果对象的类型。有一些内置合同可以处理常规场景,其中之一是 GetContent(). If you want a startActivityForResult(intent) like thing, just use ActivityResultContracts.StartActivityForResult,注册它将 return 一个 ActivityResultLauncher,在该意图内,您可以传递数组意图。

// Similar to how you pass your array with intnt before
Intent i = new Intent(MainActivity.this, NewActivity.class);
i.putExtra ("key_arr", arr); // arr is your int array
resultLauncher.launch(i);