我正在尝试使用
当我在设备上启动应用程序时,没有显示允许权限的用户界面。在启动之前,我清除了缓存并从设备中删除了该应用程序。 日志:
最后两行会一直重复,直到我关闭应用程序。 然而,当我使用
为什么在这种情况下
ActivityResultLauncher
和ActivityResultsContract.RequestMultiplePermissions
来请求权限。import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.Manifest;
import androidx.activity.result.ActivityResultLauncher;
import.androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
public class MainActivity extends AppCompatActivity {
final String[] PERMISSIONS = {
Manifest.permission.FOREGROUND_SERVICE,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_BACKGROUND_LOCATION,
Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
};
private ActivityResultContracts.RequestMultiplePermissions multiplePermissionsContract;
private ActivityResultLauncher<String[]> multiplePermissionLauncher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
multiplePermissionsContract = new ActivityResultContracts.RequestMultiplePermissions();
multiplePermissionLauncher = registerForActivityResult(multiplePermissionsContract, isGranted -> {
Log.d("PERMISSIONS", "Launcher result: " + isGranted.toString());
if (isGranted.containsValue(false)) {
Log.d("PERMISSIONS", "At least one of the permissions was not granted, launching again...");
multiplePermissionLauncher.launch(PERMISSIONS);
}
});
askPermissions(multiplePermissionLauncher);
}
private void askPermissions(ActivityResultLauncher<String[]> multiplePermissionLauncher) {
if (!hasPermissions(PERMISSIONS)) {
Log.d("PERMISSIONS", "Launching multiple contract permission launcher for ALL required permissions");
multiplePermissionLauncher.launch(PERMISSIONS);
} else {
Log.d("PERMISSIONS", "All permissions are already granted");
}
}
private boolean hasPermissions(String[] permissions) {
if (permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
Log.d("PERMISSIONS", "Permission is not granted: " + permission);
return false;
}
Log.d("PERMISSIONS", "Permission already granted: " + permission);
}
return true;
}
return false;
}
}
我还在 build.gradle
中包含了必要的依赖项:
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.activity:activity:1.2.0'
implementation 'androidx.fragment:fragment:1.3.0'
当我在设备上启动应用程序时,没有显示允许权限的用户界面。在启动之前,我清除了缓存并从设备中删除了该应用程序。 日志:
15:02:43.584 D/PERMISSIONS: Permission already granted: android.permission.FOREGROUND_SERVICE
15:02:43.586 D/PERMISSIONS: Permission is not granted: android.permission.ACCESS_FINE_LOCATION
15:02:43.586 D/PERMISSIONS: Launching multiple contract permission launcher for ALL required permissions
15:02:43.753 D/PERMISSIONS: Launcher result: {android.permission.ACCESS_FINE_LOCATION=false, android.permission.ACCESS_BACKGROUND_LOCATION=false}
15:02:43.753 D/PERMISSIONS: At least one of the permissions was not granted, launching again...
15:02:43.857 D/PERMISSIONS: Launcher result: {android.permission.ACCESS_FINE_LOCATION=false, android.permission.ACCESS_BACKGROUND_LOCATION=false}
15:02:43.857 D/PERMISSIONS: At least one of the permissions was not granted, launching again...
15:02:43.939 D/PERMISSIONS: Launcher result: {android.permission.ACCESS_FINE_LOCATION=false, android.permission.ACCESS_BACKGROUND_LOCATION=false}
最后两行会一直重复,直到我关闭应用程序。 然而,当我使用
ActivityResultContracts.RequestPermission
并分别请求每个权限时,它按预期工作,显示所有必要的用户界面。为什么在这种情况下
ActivityResultsContract.RequestMultiplePermissions
不起作用?