Created
November 20, 2016 12:46
-
-
Save isfaaghyth/c5c8aaec370f362a99c3163b4b4bc9e1 to your computer and use it in GitHub Desktop.
Runtime Permission
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package daeng.app.test.Permission; | |
import android.Manifest; | |
import android.app.Activity; | |
import android.content.pm.PackageManager; | |
import android.support.v4.app.ActivityCompat; | |
import android.support.v4.content.ContextCompat; | |
import android.widget.Toast; | |
/** | |
* Created by isfaaghyth on 14/10/16. | |
*/ | |
public class RuntimePermission { | |
public static final int RECORD_PERMISSION_REQUEST_CODE = 1; | |
public static final int EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE = 2; | |
public static final int CAMERA_PERMISSION_REQUEST_CODE = 3; | |
Activity activity; | |
public MarshMallowPermission(Activity activity) { | |
this.activity = activity; | |
} | |
public boolean checkPermissionForRecord(){ | |
int result = ContextCompat.checkSelfPermission(activity, Manifest.permission.RECORD_AUDIO); | |
if (result == PackageManager.PERMISSION_GRANTED){ | |
return true; | |
} else { | |
return false; | |
} | |
} | |
public boolean checkPermissionForExternalStorage(){ | |
int result = ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE); | |
if (result == PackageManager.PERMISSION_GRANTED){ | |
return true; | |
} else { | |
return false; | |
} | |
} | |
public boolean checkPermissionForCamera(){ | |
int result = ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA); | |
if (result == PackageManager.PERMISSION_GRANTED){ | |
return true; | |
} else { | |
return false; | |
} | |
} | |
public void requestPermissionForRecord(){ | |
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.RECORD_AUDIO)){ | |
Toast.makeText(activity, "Microphone permission needed for recording. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show(); | |
} else { | |
ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.RECORD_AUDIO},RECORD_PERMISSION_REQUEST_CODE); | |
} | |
} | |
public void requestPermissionForExternalStorage(){ | |
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)){ | |
Toast.makeText(activity, "External Storage permission needed. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show(); | |
} else { | |
ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE); | |
} | |
} | |
public void requestPermissionForCamera(){ | |
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.CAMERA)){ | |
Toast.makeText(activity, "Camera permission needed. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show(); | |
} else { | |
ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.CAMERA},CAMERA_PERMISSION_REQUEST_CODE); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment