2016-10-18 117 views
4

我想在啓動我的相機活動之前從用戶那裏獲得相機權限。在用戶從權限對話框中按下「允許」後,onRequestPermissionsResult永遠不會被回調。這裏是我的Activity類:onRequestPermissionsResult不會在Android活動中回調

public class ImageCaptureActivity extends AppCompatActivity { 

    public static final String TAG = ImageCaptureActivity.class.getSimpleName(); 

    private static final int REQUEST_CAMERA = 0; 
    private static final int REQUEST_CAMERA_PERMISSION = 1; 
    private Point mSize; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_image_capture); 

     Display display = getWindowManager().getDefaultDisplay(); 
     mSize = new Point(); 
     display.getSize(mSize); 

     // Request for Camera Permission 
     requestForCameraPermission(findViewById(android.R.id.content)); 
    } 

    /** 
    * @param view 
    * @brief requestForCameraPermission 
    */ 
    public void requestForCameraPermission(View view) { 
     Log.v(TAG, "Requesting Camera Permission"); 

     final String permission = Manifest.permission.CAMERA; 
     if (ContextCompat.checkSelfPermission(ImageCaptureActivity.this, permission) 
       != PackageManager.PERMISSION_GRANTED) { 
      if (ActivityCompat.shouldShowRequestPermissionRationale(ImageCaptureActivity.this, permission)) { 
       showPermissionRationaleDialog(getString(R.string.camera_permission_rationale), permission); 
      } else { 
       requestForPermission(permission); 
      } 
     } else { 
      launch(); 
     } 
    } 

    /** 
    * @param message 
    * @param permission 
    * @brief showPermissionRationaleDialog 
    */ 
    private void showPermissionRationaleDialog(final String message, final String permission) { 
     new AlertDialog.Builder(ImageCaptureActivity.this) 
       .setMessage(message) 
       .setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         ImageCaptureActivity.this.requestForPermission(permission); 
        } 
       }) 
       .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 

        } 
       }) 
       .create() 
       .show(); 
    } 

    /** 
    * @param permission 
    * @brief requestForPermission 
    */ 
    private void requestForPermission(final String permission) { 
     ActivityCompat.requestPermissions(ImageCaptureActivity.this, new String[]{permission}, REQUEST_CAMERA_PERMISSION); 
    } 

    /** 
    * @brief launch 
    */ 
    private void launch() { 
     Log.v(TAG, "Camera Permission Granted, launching the CameraActivity"); 
     String documentId = getIntent().getStringExtra(IntentNames.INTENT_EXTRA_WIP_DOCUMENT_ID); 
     Intent startCustomCameraIntent = new Intent(this, CameraActivity.class); 
     startCustomCameraIntent.putExtra(IntentNames.INTENT_EXTRA_WIP_DOCUMENT_ID, documentId); 
     startActivityForResult(startCustomCameraIntent, REQUEST_CAMERA); 
    } 

    @Override 
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
     switch (requestCode) { 
      case REQUEST_CAMERA_PERMISSION: 
       final int numOfRequest = grantResults.length; 
       final boolean isGranted = numOfRequest == 1 
         && PackageManager.PERMISSION_GRANTED == grantResults[numOfRequest - 1]; 
       Log.v(TAG, "Camera Permission callback on onRequestPermissionsResult"); 
       if (isGranted) { 
        launch(); 
       } 
       break; 

      default: 
       super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
     } 
    } 
} 

這裏是我的應用程序清單與攝像頭權限:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.myapp.testpackage"> 

    <uses-permission android:name="android.permission.CAMERA" /> 

    <uses-feature 
     android:name="android.hardware.camera.front" 
     android:required="true" /> 
    <uses-feature android:name="android.hardware.camera2" /> 

    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

    <application 

我的搖籃相關性:

compile 'com.android.support:appcompat-v7:24.0.0' 

compile 'com.android.support:design:24.0.0' 

compile 'com.android.support:support-v4:24.0.0' 
+0

你有'CAMERA'許可在清單中[在正確的位置](https://commonsware.com/blog/2015/08/31/hey-where-did-my-permission-go.html)? – CommonsWare

+0

@CommonsWare我已將清單信息添加到問題中。是的,我有權限。我能夠獲得許可的工作。但回調沒有被調用。 – ssk

回答

6

我想你的代碼,我自己和一切工作只是精細。我認爲這可能是一個問題的唯一的事情是該方案時,你的活動有兩個標誌:

android:noHistory="true" 
android:excludeFromRecents="true" 

這裏有一個人,是解釋問題:https://stackoverflow.com/a/35772265/5281581

+0

感謝您的嘗試。刪除 android:noHistory =「true」爲我工作。 – ssk

+0

FLAG_ACTIVITY_NO_HISTORY有以下功能。 [int FLAG_ACTIVITY_NO_HISTORY 如果設置,則新活動不會保留在歷史堆棧中。只要用戶離開它,活動就完成了。這也可以使用noHistory屬性進行設置。 如果設置,噹噹前活動啓動一個設置結果並完成的新活動時,從不會調用onActivityResult()。 ]如果調用requestPermission(),則該活動將被銷燬。所以不能得到結果。 – inhogo

相關問題