我正在开发一个QR阅读器应用程序,我在Android M及更高版本的运行时权限中需要一些帮助。
问题:我可以申请许可,甚至可以获得相机的许可。但是,包管理器导致了问题。我已从某个网站复制了运行时权限,并仅将其缩短为相机权限。
案例:当我第一次安装应用程序并单击调用相机的按钮(它要求摄像机权限)时,我授予了权限。接下来,它打开包管理器,然后选择我的设备相机。现在它打开设备相机。不是Zxing mscanner(QR框架)相机。
以下是我的代码,确定我会在这里得到一些帮助。
我的要求是:当我第一次启动应用程序时允许相机并点击“扫描”按钮时,它应该像普通的QR扫描仪一样打开。没有运行时权限,它工作正常,但现在 - 我想了解更多RTP以供将来使用。
为了进行测试,我使用的是Marshmallow和Nougat。
对于两者,我手动授予权限,我必须知道我需要使用最新设备/操作系统的运行时权限来解决这个问题。我很接近它,但我需要一些合乎逻辑的帮助。
//QR Scanner App using Zebra Crossing Framework Sample HAve to User Run time
//Permissions for only Camera for API Level 23 and above
import com.google.zxing.Result;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
//Above imports
import static android.R.attr.permission;
import static android.media.MediaRecorder.VideoSource.CAMERA;
public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
private ZXingScannerView mScannerView;
static final Integer CAMERA = 0x5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//Nougat and Marshmallow
//ask for permission
private void askForPermission(String permission, Integer requestCode) {
if (ContextCompat.checkSelfPermission(MainActivity.this, permission) != PackageManager.PERMISSION_GRANTED) {
//asking for permission prompt
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, permission)) {
//request for permissions and getting request code -- 5 for camera
ActivityCompat.requestPermissions(MainActivity.this, new String[]{permission}, requestCode);
} else {
//requesting permission
ActivityCompat.requestPermissions(MainActivity.this, new String[]{permission}, requestCode);
}
} else {
Toast.makeText(this, "" + permission + " is already granted.", Toast.LENGTH_SHORT).show();
}
}
//Response for RunTime Permissions
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(ActivityCompat.checkSelfPermission(this, permissions[0]) == PackageManager.PERMISSION_GRANTED){
switch (requestCode) {
//Camera
case 5:
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, 12);
}
// askForPermission(Manifest.permission.CAMERA,CAMERA);
break;
}
Toast.makeText(this, "Permission granted",Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show();
}
}
//Runtime Permissions Done above
//QR Scanner Invokation below
public void QrScanner(View view){
askForPermission(Manifest.permission.CAMERA,CAMERA);
mScannerView = new ZXingScannerView(this); // Programmatically initialize the scanner view
setContentView(mScannerView);
mScannerView.setResultHandler(this); for scan results.
mScannerView.startCamera(); // Start camera
}
//code ends here