简体   繁体   English

Android 6棉花糖ListView。 使用运行时权限开始活动

[英]Android 6 Marshmallow ListView. Start activity with runtime permissions

I am developing an application for Android Marshmallow (API LEvel 23). 我正在为Android棉花糖(API LEvel 23)开发应用程序。

I have an Activity which contains a ListView. 我有一个包含ListView的Activity。
I populate that ListView by using a BaseView Adapter. 我使用BaseView适配器填充该ListView。

I want to start an Activity whenever I push a button inside my ListView. 每当我在ListView中按下按钮时,我都想启动一个Activity。
However, this Activity needs to access the camera which in the latest Android version means that I should ask for the camera permission during runtime. 但是,此活动需要访问相机,这在最新的Android版本中意味着我应该在运行时请求相机许可。
The camera using Activity will be an ZXing Activity. 使用“活动”的摄像机将是ZXing活动。

Here is the Activity code: 这是活动代码:

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
        }
    }
}

The getView() method in my Adapter is: 我的适配器中的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;
}

You may have noticed that the Adapter gets an extra db argument. 您可能已经注意到,适配器获得了额外的db参数。
This is a SQLite database object which is which is used inside the Adapter, but this is not relevant. 这是一个在适配器内部使用的SQLite数据库对象,但这无关紧要。

I am not sure how to call my scan Activity by using the onClick override. 我不确定如何使用onClick覆盖调用我的扫描活动。
I tried passing the parent Activity as an argument in the Adapter and call the scan method, but I get the following exception: 我尝试将父Activity作为适配器中的参数传递并调用scan方法,但是出现以下异常:

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

Does anyone have a solution for my problem? 有人能解决我的问题吗?

You are getting java.lang.IllegalArgumentException: Can only use lower 8 bits for requestCode Error because you are using -1(PackageManager.PERMISSION_DENIED) as requestCode for requestPermissions method. 您正在获取java.lang.IllegalArgumentException: Can only use lower 8 bits for requestCode错误,因为您正在使用-1(PackageManager.PERMISSION_DENIED)作为requestPermissions方法的requestCode

To make it work use any requestCode between 1 to 255 . 要使其工作,请使用1255之间的任何requestCode

public final int got_camera = 24;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运行时异常:无法启动活动android listview - Runtime Exception: Unable to start activity android listview Android棉花糖运行时权限无法正常运行 - Android Marshmallow Runtime Permissions not working properly 安卓 列表显示。 从活动1过渡到活动2中的片段 - Android. ListView. Transition to fragment in activity2 from activity1 Android:从列表视图中点击的项目计算价格。 应用程序在总活动中崩溃 Logcat:Invalid float: "" - Android: Calculate price from clicked items in listview. App crashes in total activity Logcat:Invalid float: "" 列表视图中arrayadapter的NULL POINTER EXCEPTION。 ANDROID - NULL POINTER EXCEPTION for arrayadapter in listview. ANDROID 从ListView将JSON值解析为Activity。 初学者 - Parse JSON value to an Activity from a ListView. Beginner 在 android marshmallow (API v23) 上为 Cordova/phonegap 项目添加运行时权限 - Add runtime permissions on on a Cordova/phonegap project on android marshmallow (API v23) Android:从列表视图中删除SQLite项目。 空指针异常 - Android: delete SQLite item from listview. NullPointerException 如何授予对棉花糖上运行的android应用的权限? - How to grant permissions to android app running on Marshmallow? 如何请求 Android Marshmallow 的权限以进行 JUnit 测试 - How to request permissions on Android Marshmallow for JUnit tests
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM