正在获取 W/Activity:一次只能请求一组权限
Getting W/Activity: Can request only one set of permissions at a time
我制作了一个请求相机和 GPS 的应用程序,但是当我执行时,我多次收到此警告,每次警告的间隔不到 1 秒。
W/Activity: Can reqeust only one set of permissions at a time)
有人能告诉我为什么吗?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
mStatusCamera = intent.getStringExtra("camera");
mScannerView = new ZXingScannerView(this) {
@Override
protected IViewFinder createViewFinderView(Context context) {
return new CustomZXingScannerView(context);
}
};
List<BarcodeFormat> formats = new ArrayList<>();
mListaPassageiros = new ArrayList<>();
formats.add(BarcodeFormat.QR_CODE);
setContentView(mScannerView);
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.M) {
if (!checkPermission()) {
requestPermission();
} else {
executarDepoisDaPermissao();
}
}
}
private boolean checkPermission() {
return (ContextCompat.checkSelfPermission(getApplicationContext(), CAMERA) == PackageManager.PERMISSION_GRANTED
&& ContextCompat.checkSelfPermission(getApplicationContext(), ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED
&& ContextCompat.checkSelfPermission(getApplicationContext(), ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED);
}
public void executarDepoisDaPermissao() {
final BancoController crud = new BancoController(getBaseContext());
mConectado = isNetworkAvailable();
}
已按要求添加 RequestPermissio。
private void requestPermission() {
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.M) {
if (!checkPermission()) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION}, ASK_MULTIPLE_PERMISSION_REQUEST_CODE);
}
}
}
我可以这样使用吗?
所以,我在这里看不到你的 requestPermission()
方法,但你不应该同时发送多个权限请求。
您应该使用所有权限提出一个请求。
科特林:
val permissionsCode = 42
val permissions = arrayOf(Manifest.permission.CAMERA, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION)
if (!hasPermissions(this, permissions)) {
ActivityCompat.requestPermissions(this, permissions, permissionsCode)
}
Java:
int permissionsCode = 42;
String[] permissions = {Manifest.permission.CAMERA, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION};
if (!hasPermissions(this, permissions)) {
ActivityCompat.requestPermissions(this, permissions, permissionsCode);
}
我认为问题是您要求两个位置权限,您应该只要求精细位置,这对粗略和精细都适用。
对于遇到此问题的任何其他人..您需要连续请求权限,如下所示:
onRequestPermissionResult(){
case permission1:
if (permission1.aquired()){
....//do what you do
requestPermission2();
}
case permission2:
if (permission2.aquired()){
....//do what you do
requestPermission3();
}
}
只需在清单文件中添加权限并刷新项目
在清单中添加此代码 XML 文件:
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION/>
如果您尝试在循环中获取权限,问题可能会重现 -
do {
ActivityCompat.requestPermissions...
}while(some_condition)
在这种情况下,如果条件失败,它可能会再次请求相同的条件。我能够使用相同的指令集重现此问题。
我有同样的错误,因为我在 onCreate
AND 中调用请求权限的函数 onResume
。我认为它创建了两个权限请求实例,Android 系统不希望这样。
然后,您应该从您的 onResume
(或任何其他会 运行 而不是 Activity onCreate
的函数中删除权限请求。
我的代码用于检查访问相机和文件的权限。
protected void checkPermission(){
if(ContextCompat.checkSelfPermission(
context, Manifest.permission.READ_EXTERNAL_STORAGE)
+ ContextCompat.checkSelfPermission(
context,Manifest.permission.WRITE_EXTERNAL_STORAGE)
+ ContextCompat.checkSelfPermission(
context, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED){
// Do something, when permissions not granted
ActivityCompat.requestPermissions(
context,
new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.CAMERA
},
MY_PERMISSIONS_REQUEST_CODE
);
}else {
return;
}
}
对于结果 e 管理此 :
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_CODE: {
if (
(grantResults.length > 0) &&
(grantResults[0]
== PackageManager.PERMISSION_GRANTED
)
) {
return;
} else {
// permission was not granted
if (ActivityCompat.shouldShowRequestPermissionRationale(
context, Manifest.permission.READ_EXTERNAL_STORAGE)
|| ActivityCompat.shouldShowRequestPermissionRationale(
context, Manifest.permission.WRITE_EXTERNAL_STORAGE)
|| ActivityCompat.shouldShowRequestPermissionRationale(
context, Manifest.permission.CAMERA)) {
// user denied , but not permanently
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setIcon(R.drawable.ic_receipt);
builder.setMessage("Ops... preciso da sua permissão para incluir sua foto.");
builder.setTitle("Atenção");
builder.setPositiveButton("OK", (dialogInterface, i) -> ActivityCompat.requestPermissions(
context,
new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.CAMERA
},
MY_PERMISSIONS_REQUEST_CODE
));
builder.setNeutralButton("Cancelar", null);
AlertDialog dialog = builder.create();
dialog.show();
} else {
Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content), "You denied permission.\n" +
"Em Configurações vá em > Permissões > E ative as permissões", Snackbar.LENGTH_LONG).setAction("Configurações", view -> startActivity(new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + <**********yourpackagename*************> ))));
View snackbarView = snackbar.getView();
TextView textView = (TextView) snackbarView.findViewById(com.google.android.material.R.id.snackbar_text);
textView.setMaxLines(5); //Or as much as you need
snackbar.show();
}
}
break;
}
}
}
我制作了一个请求相机和 GPS 的应用程序,但是当我执行时,我多次收到此警告,每次警告的间隔不到 1 秒。
W/Activity: Can reqeust only one set of permissions at a time)
有人能告诉我为什么吗?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
mStatusCamera = intent.getStringExtra("camera");
mScannerView = new ZXingScannerView(this) {
@Override
protected IViewFinder createViewFinderView(Context context) {
return new CustomZXingScannerView(context);
}
};
List<BarcodeFormat> formats = new ArrayList<>();
mListaPassageiros = new ArrayList<>();
formats.add(BarcodeFormat.QR_CODE);
setContentView(mScannerView);
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.M) {
if (!checkPermission()) {
requestPermission();
} else {
executarDepoisDaPermissao();
}
}
}
private boolean checkPermission() {
return (ContextCompat.checkSelfPermission(getApplicationContext(), CAMERA) == PackageManager.PERMISSION_GRANTED
&& ContextCompat.checkSelfPermission(getApplicationContext(), ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED
&& ContextCompat.checkSelfPermission(getApplicationContext(), ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED);
}
public void executarDepoisDaPermissao() {
final BancoController crud = new BancoController(getBaseContext());
mConectado = isNetworkAvailable();
}
已按要求添加 RequestPermissio。
private void requestPermission() {
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.M) {
if (!checkPermission()) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION}, ASK_MULTIPLE_PERMISSION_REQUEST_CODE);
}
}
}
我可以这样使用吗?
所以,我在这里看不到你的 requestPermission()
方法,但你不应该同时发送多个权限请求。
您应该使用所有权限提出一个请求。
科特林:
val permissionsCode = 42
val permissions = arrayOf(Manifest.permission.CAMERA, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION)
if (!hasPermissions(this, permissions)) {
ActivityCompat.requestPermissions(this, permissions, permissionsCode)
}
Java:
int permissionsCode = 42;
String[] permissions = {Manifest.permission.CAMERA, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION};
if (!hasPermissions(this, permissions)) {
ActivityCompat.requestPermissions(this, permissions, permissionsCode);
}
我认为问题是您要求两个位置权限,您应该只要求精细位置,这对粗略和精细都适用。
对于遇到此问题的任何其他人..您需要连续请求权限,如下所示:
onRequestPermissionResult(){
case permission1:
if (permission1.aquired()){
....//do what you do
requestPermission2();
}
case permission2:
if (permission2.aquired()){
....//do what you do
requestPermission3();
}
}
只需在清单文件中添加权限并刷新项目
在清单中添加此代码 XML 文件:
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION/>
如果您尝试在循环中获取权限,问题可能会重现 -
do {
ActivityCompat.requestPermissions...
}while(some_condition)
在这种情况下,如果条件失败,它可能会再次请求相同的条件。我能够使用相同的指令集重现此问题。
我有同样的错误,因为我在 onCreate
AND 中调用请求权限的函数 onResume
。我认为它创建了两个权限请求实例,Android 系统不希望这样。
然后,您应该从您的 onResume
(或任何其他会 运行 而不是 Activity onCreate
的函数中删除权限请求。
我的代码用于检查访问相机和文件的权限。
protected void checkPermission(){
if(ContextCompat.checkSelfPermission(
context, Manifest.permission.READ_EXTERNAL_STORAGE)
+ ContextCompat.checkSelfPermission(
context,Manifest.permission.WRITE_EXTERNAL_STORAGE)
+ ContextCompat.checkSelfPermission(
context, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED){
// Do something, when permissions not granted
ActivityCompat.requestPermissions(
context,
new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.CAMERA
},
MY_PERMISSIONS_REQUEST_CODE
);
}else {
return;
}
}
对于结果 e 管理此 :
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_CODE: {
if (
(grantResults.length > 0) &&
(grantResults[0]
== PackageManager.PERMISSION_GRANTED
)
) {
return;
} else {
// permission was not granted
if (ActivityCompat.shouldShowRequestPermissionRationale(
context, Manifest.permission.READ_EXTERNAL_STORAGE)
|| ActivityCompat.shouldShowRequestPermissionRationale(
context, Manifest.permission.WRITE_EXTERNAL_STORAGE)
|| ActivityCompat.shouldShowRequestPermissionRationale(
context, Manifest.permission.CAMERA)) {
// user denied , but not permanently
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setIcon(R.drawable.ic_receipt);
builder.setMessage("Ops... preciso da sua permissão para incluir sua foto.");
builder.setTitle("Atenção");
builder.setPositiveButton("OK", (dialogInterface, i) -> ActivityCompat.requestPermissions(
context,
new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.CAMERA
},
MY_PERMISSIONS_REQUEST_CODE
));
builder.setNeutralButton("Cancelar", null);
AlertDialog dialog = builder.create();
dialog.show();
} else {
Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content), "You denied permission.\n" +
"Em Configurações vá em > Permissões > E ative as permissões", Snackbar.LENGTH_LONG).setAction("Configurações", view -> startActivity(new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + <**********yourpackagename*************> ))));
View snackbarView = snackbar.getView();
TextView textView = (TextView) snackbarView.findViewById(com.google.android.material.R.id.snackbar_text);
textView.setMaxLines(5); //Or as much as you need
snackbar.show();
}
}
break;
}
}
}