[英]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 . 要使其工作,请使用1到255之间的任何
requestCode
。
public final int got_camera = 24;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.