1

I'm trying to request permissions, it's throwing an unknown method for registerForActivityResult, at first I was using regular android.app.Activity, because, due to lack of a PC at the moment, I've been using AIDE, and I was having issues with AppCompatActivity before, but I noticed that the registerForActivityResult() method belongs to ActivityResultCaller which isn't implemented by Activity but is implemented by AppCompatActivity, so I switched my main activity over to extending AppCompatActivity, along the way I've changed out a bunch of imports and dependencies, and can't get it to work, I also tried to just implement ActivityResultCaller, but then that error goes away and everything fits, but then it says I'm not implementing the abstract method registerForActivityResult, which I am and now has no red lines or errors around it, so I went back to not implementing it and trying to get it to work with plain appcompatactivity, because in all the tutorials that I saw they weren't having to state that MainActivityimplements ActivityResultCaller, so I messed around more with the dependencies, at this point I don't really remember all that I've done, so I probably have some in there now that isn't necessary, I've cut away most of the code to make it simpler, but this is where I'm at now, any suggestions?

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

And here's the build.gradle file

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"
}
Michael
  • 33
  • 8
  • How old is this project? Google rebranded support library years ago and `compile` keyword is deprecated for years and recently removed. – Shlomi Katriel Jan 09 '22 at 05:20
  • It's a lil old, but its pobably mostly because im going off of old tutorials, will it not work using it – Michael Jan 09 '22 at 05:27
  • Does this answer your question? [AndroidX ActivityResultContracts package not found / class not found](https://stackoverflow.com/questions/61622254/androidx-activityresultcontracts-package-not-found-class-not-found) – AgentP Jan 09 '22 at 05:53
  • I would recommend upgrading the compileSDKVersion to a recent one. I have used AIDE in the past but don't know to what extent does it support. – Sujal Kumar Jan 09 '22 at 06:01
  • I upgraded compileSDKVersion min & target to 30, and buildToolsVersion to 30.0.2, still nothing – Michael Jan 10 '22 at 02:13
  • And AgentP, with that other question it wasnt finding ActivityResultContract, and theyre suggestion was to add androidx.activity:activity:1.2.0 or higher to dependencies, I'm using 1.4.0 at the suggestion of somebody else, and it's still not working – Michael Jan 10 '22 at 02:39
  • Did you find any solution for this? – K. Ajay Apr 27 '22 at 09:27

2 Answers2

1

As suggested in the comments the code you are using including dependencies is old. Look into androidx which has replaced support libraries. 'compile' is also deprecated in favour of 'implementation. That being said I have included sample code for requesting permissions with Results Activity API.

check whether permissions are granted for Android 6 or greater

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

Activity Result API for one permission

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

For multiple permissions

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

Latest stable activity dependency

implementation 'androidx.activity:activity:1.4.0'

For fragments

implementation 'androidx.fragment:fragment:1.4.0'
SABANTO
  • 1,316
  • 9
  • 24
  • 1
    Ok I replaced all the compiles with implementation, added that first bit to my checkPermissions method, and replaced my activityResultLauncher with yours, and added that dependency for activities, I also updated the compileSDKVersion as well as the min and target to 30, and the buildToolsVersion to 30.0.2, but it's still not recognizing registerforActivityResult – Michael Jan 10 '22 at 02:20
1

can you use this method instead of ActivityResualtContract.RequestPermission... clean project and rebuild your project then try this method

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