Continue code after accepting permissions, Android Studio

Asked

Viewed 79 times

0

Good night. I am trying to make a page just to request the necessary permissions and then forward users to another application activity, however I am not able to make after accepting the permissions it is forwarded, I have to go out and enter the application or rotate it to forward to the next page. I’m a layman in that language if anyone can help me, I’d appreciate it. Below the application code.

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class Inico extends AppCompatActivity {

    TextView txtpermissao;

    String[] appPermissoes = {
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.CAMERA
    };

    public static final int CODIGO_PERMISSOES_REQUERIDAS = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inico);

        txtpermissao = findViewById(R.id.txtpermissao);

        if(verificarPermissoes()){
            Intent intent = new Intent(this, SegundaActivity.class);
            startActivity(intent);
        }else {
            txtpermissao.setText("Nem todas as Permissões Foram Abilitadas");
        }
    }
    public boolean verificarPermissoes(){
        List<String> permissoesRequeridas = new ArrayList<>();
        for(String permissao : appPermissoes){
            if(ContextCompat.checkSelfPermission(this, permissao) != PackageManager.PERMISSION_GRANTED) {
                permissoesRequeridas.add(permissao);
            }
        }
            if(!permissoesRequeridas.isEmpty()){
                ActivityCompat.requestPermissions(this,
                        permissoesRequeridas.toArray(new String[permissoesRequeridas.size()]),
                        CODIGO_PERMISSOES_REQUERIDAS);
                return false;
            }
                return true;
    };

}```

1 answer

2

Come on!

To implement the flow of resource permission requests on Android you need to pay attention to a few points:

  1. You should always check if permission has already been guaranteed.
  2. You should always ask permission
  3. And lastly, you should always wait for the reply of the permission request

The two starting points you covered in your code. You have implemented the Request() method that checks whether these requested permissions have already been guaranteed, and if they are not, you request them.

What is missing from your code is point 3: You are not waiting for an answer to your request, and therefore have no way of knowing whether permission has been granted or not.

To do this is simple. In your Activity, add the code below:

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    /*INSIRA AQUI NOVAMENTE A LOGICA QUE VERIFICA AS PERMISSOES*/
    /*AS PERMISSOES E SEUS RESULTADOS VEM NOS ARRAYS permissions e grantResults*/


}

If the user denies one or more permissions it is a good practice to present a screen or popup explaining to him why his app needs that permission before requesting it again.

For a full example, take a look at repository of official android documentation by clicking here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.