我搜索了类似的问题,并遵循了答案中的方法,但没有一个对我有用。我知道以前曾问过这样的问题。我正在Android 6.0 API 23上测试我的应用程序。单击“允许授予权限”但从不调用onRequestPermissionsResult时,权限对话框打开。无论我单击“允许”还是“拒绝”,都不会在屏幕上显示任何吐司。我知道我错过了一些东西,但我无法弄清楚。
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
Camera camera;
Preview preview;
ImageButton buttonClick;
ProgressBar progressBar;
public static Bitmap bitmap;
@Override
public void onCreate(Bundle savedInstanceState) {
Log.e(TAG,"onCreate");
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar) findViewById(R.id.progressbar);
buttonClick = (ImageButton) findViewById(R.id.imageButton);
progressBar.setVisibility(View.GONE);
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
//// if API >= 23 equest permisions
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED)) {
ActivityCompat.requestPermissions(MainActivity.this,new String[]{"android.permission.CAMERA" }, 0);
}else{
moveOn();
}
buttonClick.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
progressBar.setVisibility(View.VISIBLE);
AudioManager mgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
mgr.setStreamMute(AudioManager.STREAM_SYSTEM, true);
preview.camera.takePicture(null, rawCallback, jpegCallback);
}
});
}
private void moveOn() {
preview = new Preview(this);
((FrameLayout) findViewById(R.id.preview)).addView(preview);
}
@Override
protected void onPause() {
super.onPause();
finish();
}
PictureCallback rawCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
Log.e(TAG,"onPictureTaken in rawCallback ");
}
};
PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
Log.e(TAG,"onPictureTaken in jpegCallback ");
camera.stopPreview();
//finish();
Intent k = new Intent(MainActivity.this, ImageSet.class);
startActivity(k);
progressBar.setVisibility(View.GONE);
}
};
//This method will be called when the user will tap on allow or deny
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//Checking the request code of our request
if(requestCode == 0){
//If permission is granted
if(grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
//Displaying a toast
Toast.makeText(this,"Permission granted to use Camera",Toast.LENGTH_LONG).show();
}else{
//Displaying another toast if permission is not granted
Toast.makeText(this,"Oops you just denied the permission",Toast.LENGTH_LONG).show();
}
}
}
最佳答案
@Override
protected void onPause() {
super.onPause();
finish();
}
显示请求对话框时,由于您的活动失去了焦点,因此该方法将回调,并且您完成了活动。 onRequestPermissionsResult将不会回叫。
我不知道您为什么要用这种方法完成活动,所以解决方案是删除finish()语句。