0

This code is for testing whether application has Permission after being checked would be returned true if you have permission, and false otherwise .

But it is not working when I run application stops working , so I wonder why it's not working , if this missing something in the code:

EDIT2 - Made some corrections with suggestions, but still didn't work:

 public class CheckPermission extends Activity {
    private final Context context;
    private static final String MNC = "MNC";
    int permReq = 0;
    String permMan = "";

    public CheckPermission(Context context) {
        this.context = context;
    }

    //perMan can be any code number higher than 0
    public void requestPermission(String permRequested){
        switch (permRequested) {
            case "CAMERA":
                //Request for Camera
                this.permReq =  ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA);
                this.permMan = Manifest.permission.CAMERA;
                break;
            case "INTERNET":
                //Requesr for Internet
                this.permReq =  ContextCompat.checkSelfPermission(context, Manifest.permission.INTERNET);
                this.permMan = Manifest.permission.INTERNET;
                break;
            case "STORAGE":
                //Request for group Storage - Read_External_Storage & Write_External_Storage
                this.permReq =  ContextCompat.checkSelfPermission(context, Manifest.permission_group.STORAGE);
                this.permMan = Manifest.permission_group.STORAGE;
                break;
            case "MICROPHONE":
                //Request for group Microphone - Record_Audio
                this.permReq =  ContextCompat.checkSelfPermission(context, Manifest.permission_group.MICROPHONE);
                this.permMan = Manifest.permission_group.MICROPHONE;
                break;
            case "LOCATION":
                //Request for group Location - Acess_Fine_Location & Acess_Coarse_Location
                this.permReq =  ContextCompat.checkSelfPermission(context, Manifest.permission_group.LOCATION);
                this.permMan = Manifest.permission_group.LOCATION;
                break;
            case "CALL":
                //Requesr for call
                this.permReq = ContextCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE);
                this.permMan = Manifest.permission.CALL_PHONE;
                break;
            default:
                break;
        }
    }

    public boolean hasPermission( String permRequested){
        final PackageManager pm = context.getPackageManager();

        if(isMNC_Or_Higher()) {
            requestPermission(permRequested);
            Toast.makeText(this.context, "Is MNC - permMan: " + this.permMan + " Perm required: " + permReq, Toast.LENGTH_SHORT).show();

            if (permReq != pm.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, new String[]{this.permMan}, this.permReq);
                return false;
            }
        }
        return true;
    }

    //check if is 6.0 or higher
    public boolean isMNC_Or_Higher(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            return true;
        }
        return false;
    }

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

        if (requestCode == this.permReq) {
            if (grantResults.length == 1 &&
                    grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "Permissão concedida", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "Permissão negada", Toast.LENGTH_SHORT).show();
            }
        } else {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
}

ERROR:

PID: 25504 java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:107) at com.dev.kamui.patrulhacomunitaria.CheckPermission.hasPermission(CheckPermission.java:68) at com.dev.kamui.patrulhacomunitaria.Pagina_Principal$1.onClick(Pagina_Principal.java:47) at android.view.View.performClick(View.java:5198) at android.view.View$PerformClick.run(View.java:21147) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Hiago Vitor
  • 3
  • 1
  • 3

3 Answers3

1

requestPermissions needs to be called like this

requestPermissions(this, new String[]{this.permMan}, this.permReq);

If you want CheckPermission as Util class then there is no need to inherit it from Activity class. Also no need to do "this.context = context;" in CheckPermission constructor. Instead send the context through hasPermission method and into requestPermission method. Use it in ActivityCompat.requestPermissions also. Then implement onRequestPermissionsResult inside each activity that is calling hasPermission.

Bharath Kumar
  • 541
  • 3
  • 10
  • I'm getting an error in checkSelfPermission, whenever i use this code the application stop running. I tried using `ActivityCompat.requestPermissions(this, new String[]{this.permMan}, this.permReq);` but didn't solve the problem. – Hiago Vitor Apr 02 '16 at 17:54
  • Could you try: checkSelfPermission (this, permission); It would be good to add logs of the error. – Bharath Kumar Apr 02 '16 at 17:57
  • Didn't work i post the error in logcat after the code – Hiago Vitor Apr 02 '16 at 19:04
  • Seems you are not getting the applicationcontext. You would need to pass a proper context. Refer http://stackoverflow.com/questions/10641144/ And check line number 68 in CheckPermission.java – Bharath Kumar Apr 02 '16 at 19:20
  • Just one question what i want to do here is, call one method from this activity from other, because in many activitys i need to check if the user has granted permission (android 6.0 - 23api). Do you think in this scenerio i 'm doing something wrong? For exmaple i shouldn't use activity to do this, because what i want is a Util class – Hiago Vitor Apr 02 '16 at 22:29
  • If you want CheckPermission as Util class then there is no need to inherit it from Activity class. Also no need to do "this.context = context;" in CheckPermission constructor. Instead send the context through hasPermission method and into requestPermission method. Use it in ActivityCompat.requestPermissions also. Then implement onRequestPermissionsResult inside each activity that is calling hasPermission. Hope that will help. – Bharath Kumar Apr 04 '16 at 14:43
  • I was trying to make all in util class, but din't work, i will try what you "said" to se if it work. Just one quest the "Permission Code" can be anything ? – Hiago Vitor Apr 17 '16 at 22:10
  • Do you mean requestCode? Yes it can be any integer value, but it needs to be specific. And the same value will be passed to onRequestPermissionsResult. – Bharath Kumar Apr 20 '16 at 15:15
0

Activity should not have such constructor as you wrote.

Activity by itself is a subclass of context so you can write this instead of context:

this.permReq = ContextCompat.checkSelfPermission(this, Manifest.permission_group.STORAGE);
  • Than i sohul just use `@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); }` instead right? – Hiago Vitor Apr 02 '16 at 19:46
0

From Android 6.0 and higher you have to request permission in code.

private static final int PERMISSION_REQUEST_CODE = 1;

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {

    if (checkSelfPermission(Manifest.permission.SEND_SMS)
            == PackageManager.PERMISSION_DENIED) {

        Log.d("permission", "permission denied to SEND_SMS - requesting it");
        String[] permissions = {Manifest.permission.SEND_SMS};

        requestPermissions(permissions, PERMISSION_REQUEST_CODE);

    }
}
Tru Nguyen
  • 11
  • 2