在Activity中未调用onRequestPermissionsResult

时间:2016-10-24 23:28:57

标签: android android-6.0-marshmallow

我搜索了类似的问题并按照答案中的方法进行了搜索,但没有一个对我有效。我知道以前曾问过这样的问题。我在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();
        }
    }
}

1 个答案:

答案 0 :(得分:3)

@Override
protected void onPause() {
    super.onPause();
    finish();
}

当请求对话框显示时,此方法将回调,因为您的活动失去了焦点,并且您完成了您的活动。 onRequestPermissionsResult 不会回电。

我不知道你为什么要用这种方法完成你的活动,所以解决办法就是删除 finish() sentance。