未知方法 registerForActivityResult

我正在尝试请求权限,它为 registerForActivityResult 抛出了一个未知的方法,起初我使用的是常规的 android.app.Activity,因为目前由于缺少 PC,我一直在使用 AIDE,我之前遇到过 AppCompatActivity 的问题,但我注意到 registerForActivityResult() 方法属于 ActivityResultCaller 不是由 Activity 实现但由 AppCompatActivity 实现的,所以我切换了我的主要活动在扩展 AppCompatActivity 的过程中,我已经更改了一堆导入和依赖项,但无法让它工作,我也尝试只实现 ActivityResultCaller ,但随后该错误消失了一切都合适,但它说我没有实现抽象方法 registerForActivityResult ,我现在没有红线或错误,所以我回到不实现它并试图让它工作简单的 appcompatactivity ,因为在我看到的所有教程中,他们不必说明 MainActivity 实现 ActivityResultCaller ,所以我在依赖项上搞砸了更多,此时我真的不记得了所有吨我已经完成了,所以我现在可能有一些不必要的,我已经删除了大部分代码以使其更简单,但这就是我现在的位置,有什么建议吗?

    package com.mycompany.MyApp;

import android.app.*;

import android.os.*;
import android.view.Window;
import android.view.WindowManager;
import android.support.v4.content.*;
import android.content.Context;
import androidx.activity.result.*;
import android.provider.*;
import androidx.core.app.*;
import android.content.Intent;
import android.net.Uri;
import android.content.pm.*;
import androidx.activity.result.contract.*;
import androidx.activity.*;
import android.provider.*;
import java.security.*;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.*;

public class MainActivity extends AppCompatActivity{

    String[] permissions = {android.Manifest.permission.READ_EXTERNAL_STORAGE, android.Manifest.permission.WRITE_EXTERNAL_STORAGE};
    public static MainActivity instance;

    public ActivityResultLauncher<Intent> activityResultLauncher;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        activityResultLauncher =
        registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(), 
            new ActivityResultCallback<ActivityResult>(){
                    @Override
                    public void onActivityResult(ActivityResult result){
                }
        });

        setContentView(  new Game(this));
    }
}

这是 build.gradle 文件

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.0"

    defaultConfig {
        applicationId "com.mycompany.MyApp"
        minSdkVersion 14
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation "androidx.activity:activity:1.4.0"
    implementation "androidx.activity:activity:1.2.0-rc01"

    compile fileTree(dir: 'libs', include: ['*.jar'])
    Implementation "com.android.support:support-v4:23.+"
    Implementation "com.android.support:appcompat-v7:22.1.0"
}
stack overflow Unknown method registerForActivityResult
原文答案

答案:

作者头像

正如评论中所建议的,您使用的包含依赖项的代码是旧的。查看已替换支持库的 androidx。 'compile' 也被弃用,取而代之的是 'implementation.话虽如此,我已经包含了用于使用结果活动 API 请求权限的示例代码。

检查是否为 Android 6 或更高版本授予权限

    if (Build.VERSION.SDK_INT >= 23) {
                if (ActivityCompat.checkSelfPermission(requireContext(), permissions) != PackageManager.PERMISSION_GRANTED) {
//permissions is a string array for multiple permission else single permission
                    getPermission.launch(permissions);
                }
            }

一项权限的活动结果 API

private final ActivityResultLauncher<String> getPermission =
        registerForActivityResult(new ActivityResultContracts.RequestPermission(), new ActivityResultCallback<Boolean>() {
            @Override
            public void onActivityResult(Boolean isGranted) {
                if (isGranted) {
                    //PERMISSION GRANTED DO SOMETHING
                } else {
                    //EXPLAIN TO USER WHY PERMISSION ARE NECESSARY FOR FUNCTINALITY
                }
            }
        });

对于多个权限

private final ActivityResultLauncher<String[]> locationPermissions =
        registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), result -> {
            if (result.containsValue(false)) {
                //one of the permissions is not granted, use result[0] to get result for individual permissions
            } else {
                //all permissions granted do something
            }
        });

最新的稳定活动依赖

implementation 'androidx.activity:activity:1.4.0'

对于片段

implementation 'androidx.fragment:fragment:1.4.0'
作者头像

你能用这个方法代替 ActivityResualtContract.RequestPermission... 清理项目并重建你的项目然后试试这个方法

ActivityResultLauncher<String> storagePermissionLauncher = registerForActivityResult(new ActivityResultCallback<String> () {
            /**
             * Called when result is available
             *
             * @param result
             */
            @Override
            public void onActivityResult(String result) {
            }
            });