Added permission request

This commit is contained in:
Michael Schättgen 2016-08-15 22:35:42 +02:00
parent 3df18c0226
commit f508eb6645

View file

@ -1,9 +1,13 @@
package me.impy.aegis;
import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.google.zxing.Result;
@ -17,13 +21,13 @@ public class ScannerActivity extends Activity implements ZXingScannerView.Result
super.onCreate(state);
mScannerView = new ZXingScannerView(this); // Programmatically initialize the scanner view
setContentView(mScannerView); // Set the scanner view as the content view
ActivityCompat.requestPermissions(ScannerActivity.this, new String[]{Manifest.permission.CAMERA}, 1);
}
@Override
public void onResume() {
super.onResume();
mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
mScannerView.startCamera(); // Start camera on resume
}
@Override
@ -41,4 +45,25 @@ public class ScannerActivity extends Activity implements ZXingScannerView.Result
// If you would like to resume scanning, call this method below:
mScannerView.resumeCameraPreview(this);
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
mScannerView.startCamera(); // Start camera on resume
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
Toast.makeText(ScannerActivity.this, "Permission denied to get access to the camera", Toast.LENGTH_SHORT).show();
}
return;
}
}
}
}