Android Permission
1 minute read
AndroidManifest.xml
<!-- Storage -->
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Camera -->
<uses-permission android:name="android.permission.CAMERA" />
<!--<uses-permission android:name="android.permission.FLASHLIGHT" />-->
<uses-feature android:name="android.hardware.camera" android:required="true"/>
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/>
<uses-feature android:name="android.hardware.camera.front" android:required="true" />
MainActivity.java
Activity mActivity = this;
// ----------------------------------------------
// Permission
// ----------------------------------------------
static final int REQUEST_CAMERA = 1;
static final int REQUEST_WRITE_EXTERNAL_STORAGE = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
grantPermission(android.Manifest.permission.CAMERA, REQUEST_CAMERA);
}
/**
* request permission
* @param perm_str android.Manifest.permission.xxx
* @param req_code REQUEST_xxx
*/
private boolean grantPermission(String perm_str, int req_code) {
int permission = ContextCompat.checkSelfPermission(mActivity, perm_str);
if(permission != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(mActivity, new String[]{perm_str}, req_code);
return false;
}
return true;
}
/**
* Receive permission result
* @param requestCode
* @param permissions
* @param grantResults
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
int idx;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
switch (requestCode) {
case REQUEST_CAMERA:
for (idx = 0; idx < permissions.length; idx++) {
String permission = permissions[idx];
int grantResult = grantResults[idx];
if (permission.equals(android.Manifest.permission.CAMERA) && grantResult == PackageManager.PERMISSION_GRANTED) {
Log.v(TAG, "CAMERA Permitted");
// Restart this application
PackageManager packageManager = context.getPackageManager();
Intent intent = packageManager.getLaunchIntentForPackage(context.getPackageName());
ComponentName componentName = intent.getComponent();
Intent mainIntent = Intent.makeRestartActivityTask(componentName);
context.startActivity(mainIntent);
System.exit(0);
break;
}
}
if (idx == permissions.length) {
ShowEndProgram(); // End this program
}
case REQUEST_WRITE_EXTERNAL_STORAGE:
for (idx = 0; idx < permissions.length; idx++) {
String permission = permissions[idx];
int grantResult = grantResults[idx];
if (permission.equals(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) && grantResult == PackageManager.PERMISSION_GRANTED) {
Log.v(TAG, "WRITE_EXTERNAL_STORAGE Permitted");
break;
}
}
}
}
}
/**
* Confirm stop or not
*/
private void ShowEndProgram() {
if (mActivity.isFinishing()) return; // to prevent BadTokenException
if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.CAMERA)) {
showToastLong(getResources().getString(R.string.msg_need_camera_permission));
startInstalledAppDetailsActivity(mActivity);
finish();
}
}
/**
* Shows a {@link Toast} on the UI thread.
*
* @param text The message to show
*/
private void showToastLong(final String text) {
final Activity activity = mActivity;
if (activity != null) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(activity, text, Toast.LENGTH_LONG).show();
}
});
}
}
Leave a comment