我需要将新的权限添加到旧项目中,这仍然需要坚持使用ant构建。以下是以前的配置:
project.properties:
target=android-23
的AndroidManifest.xml:
<uses-sdk android:minSdkVersion="13" />
我将AndroidManifest.xml更改为
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="23" />
但发现在我的Android 6.0手机上它仍然警告我,我的应用程序与最新的Android权限要求不兼容。而且它的行为就像它真正针对23之前的那样。
那么有什么解决方案吗?
答案 0 :(得分:0)
我只是共享代码来添加权限检查,这是自Marshnmallow以来已经强制执行的程序。例如,我检查是否允许访问摄像头。
private void launchCamera() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(MyActivity.this,
Manifest.permission.CAMERA)) {
// Showing explanation to user to require permission
// <Build a dialog here. Following shows how to handle dialog clicks>
dialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Yes, user wants to give permission
dialog.dismiss();
ActivityCompat.requestPermissions(MyActivity.this,
new String[] {Manifest.permission.CAMERA},
Constants.PERMISSION_CAMERA);
}
});
alog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// No, user does not want to give permission now
dialog.dismiss();
}
});
} else {
// No explanation needed, requesting for permission now.
ActivityCompat.requestPermissions(this,
new String[] {Manifest.permission.CAMERA},
Constants.PERMISSION_CAMERA);
}
} else {
// Permission to get location has been granted already.
try {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, Constants.CAMERA_CAPTURE_REQUEST);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
Log.e(TAG, "Camera is not found!");
}
}
}
接下来,我们有一个方法可以根据用户提供给系统对话框的响应来实现(在我们的对话框之后弹出)。
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == Constants.PERMISSION_CAMERA ){
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
launchCamera();
} else if (grantResults[0] == PackageManager.PERMISSION_DENIED){
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.CAMERA)) {
/*Here we have to show a dialog/ something else to show user why app requires permission. This is a good practice.*/
// Handle dialog clicks again
// Build a dialog here
btnPositiveOfDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Yes, I want to give permission now after reading detailed message
dialog.dismiss();
ActivityCompat.requestPermissions(UserInfoActivity.this,
new String[] {Manifest.permission.CAMERA},
Constants.PERMISSION_CAMERA);
}
});
btnNegativeOfDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// No, I still don't understand detailed message and want give permission away
dialog.dismiss();
}
});
} else {
// User checked 'Never ask again'
// Build a dialog here
btnPositiveOfDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Yes, go to app settings and manually enable settings
dialog.dismiss();
final Intent i = new Intent();
i.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
i.addCategory(Intent.CATEGORY_DEFAULT);
i.setData(Uri.parse("package:" + UserInfoActivity.this.getPackageName()));
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
i.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
MyActivity.this.startActivity(i);
}
});
btnNegativeOfDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// No, I don't want to go to user settings and give permission now.
dialog.dismiss();
}
});
dialog.show();
}
}
}
}