Android 6 棉花糖列表视图。使用运行时权限启动 activity

Android 6 Marshmallow ListView. Start activity with runtime permissions

我正在为 Android Marshmallow(API 级别 23)开发一个应用程序。

我有一个 Activity,其中包含一个 ListView。
我使用 BaseView 适配器填充该 ListView。

每当我在 ListView 中按下按钮时,我想启动一个 Activity。
然而,这个Activity需要访问相机,在最新的Android版本中意味着我应该在运行时请求相机权限。
使用 Activity 的相机将是 ZXing Activity.

这里是 Activity 代码:

public class ProviderListActivity extends AppCompatActivity {

    public boolean can_access_camera = false;
    public final int got_camera = PackageManager.PERMISSION_DENIED;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_provider_list);
        ListView listView = (ListView) findViewById(R.id.ProviderListView);
        CardsDBHelper dbHelper = new CardsDBHelper(this);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        listView.setAdapter(new ProviderListAdapter(this, db));
    }

    public void start_scan(){
        if (have_camera_permission()){
            start_scan_activity();
        } else {
            Log.d("ProviderListActivity", "I CANNOT ACCESS THE CAMERA");
        }
    }

    public void start_scan_activity(){
        IntentIntegrator integrator = new IntentIntegrator(this);
        integrator.initiateScan();
    }

    public boolean have_camera_permission(){
        //Check for permissions
        int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
        if (permissionCheck == PackageManager.PERMISSION_DENIED){
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, got_camera);
        } else {
            can_access_camera = true;
        }
        return can_access_camera;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case got_camera: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    can_access_camera = true;
                } else {
                    can_access_camera = false;
                }
                return;
            }
            // other 'case' lines to check for other
            // permissions this app might request
        }
    }
}

我的 Adapter 中的 getView() 方法是:

public View getView(int position, View convertView, final ViewGroup parent) {
Button grid_button;
    if (convertView == null) {
        // if it's not recycled, initialize some attributes
        grid_button = new Button(mContext);
        grid_button.setText("Hello Text");
        Drawable d = mContext.getDrawable(R.drawable.circle);
    grid_button.setCompoundDrawablesRelativeWithIntrinsicBounds(null,d,null,null);
    grid_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //What do I need to put inside here?
        }
    });
} else {
    grid_button = (Button) convertView;
}

return grid_button;
}

您可能已经注意到 Adapter 有一个额外的 db 参数。
这是适配器内部使用的 SQLite 数据库对象,但这不相关。

我不确定如何使用 onClick 覆盖调用我的扫描 Activity。
我尝试将父 Activity 作为适配器中的参数传递并调用扫描方法,但出现以下异常:

java.lang.IllegalArgumentException: Can only use lower 8 bits for requestCode` 

有人能解决我的问题吗?

您收到 java.lang.IllegalArgumentException: Can only use lower 8 bits for requestCode 错误,因为您将 -1(PackageManager.PERMISSION_DENIED) 作为 requestCode 用于 requestPermissions 方法。

要使其工作,请使用 1255.

之间的任何 requestCode
public final int got_camera = 24;