gpt4 book ai didi

Android 6.0 权限错误

转载 作者:IT老高 更新时间:2023-10-28 23:09:19 25 4
gpt4 key购买 nike

我收到此错误:

getDeviceId: Neither user 10111 nor current process has android.permission.READ_PHONE_STATE.
at android.os.Parcel.readException(Parcel.java:1599)
at android.os.Parcel.readException(Parcel.java:1552)

我已经在 list 中给出了它。 Android 6.XX Marshmallow 设备有什么变化吗?我需要授予 READ_PHONE_STATE 权限以获取设备的 IMEI。请。帮助。

最佳答案

是的,Android M 上的权限已更改。现在在运行时请求权限,而不是在 Android M 之前的安装时间。

您可以查看文档 here

This release introduces a new permissions model, where users can now directly manage app permissions at runtime. This model gives users improved visibility and control over permissions, while streamlining the installation and auto-update processes for app developers. Users can grant or revoke permissions individually for installed apps.

On your apps that target Android 6.0 (API level 23) or higher, make sure to check for and request permissions at runtime. To determine if your app has been granted a permission, call the new checkSelfPermission() method. To request a permission, call the new requestPermissions() method. Even if your app is not targeting Android 6.0 (API level 23), you should test your app under the new permissions model.

For details on supporting the new permissions model in your app, see Working with System Permissionss. For tips on how to assess the impact on your app, see Permissions Best Practices.

要检查权限,您必须像这样检查,取自 github

public class MainActivity extends SampleActivityBase
implements ActivityCompat.OnRequestPermissionsResultCallback {

public static final String TAG = "MainActivity";

/**
* Id to identify a camera permission request.
*/
private static final int REQUEST_CAMERA = 0;

// Whether the Log Fragment is currently shown.
private boolean mLogShown;

private View mLayout;

/**
* Called when the 'show camera' button is clicked.
* Callback is defined in resource layout definition.
*/
public void showCamera(View view) {
// Check if the Camera permission is already available.
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
// Camera permission has not been granted.

requestCameraPermission();

} else {

// Camera permissions is already available, show the camera preview.
showCameraPreview();
}
}

/**
* Requests the Camera permission.
* If the permission has been denied previously, a SnackBar will prompt the user to grant the
* permission, otherwise it is requested directly.
*/
private void requestCameraPermission() {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.CAMERA)) {
// Provide an additional rationale to the user if the permission was not granted
// and the user would benefit from additional context for the use of the permission.
// For example if the user has previously denied the permission.
Snackbar.make(mLayout, R.string.permission_camera_rationale,
Snackbar.LENGTH_INDEFINITE)
.setAction(R.string.ok, new View.OnClickListener() {
@Override
public void onClick(View view) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.CAMERA},
REQUEST_CAMERA);
}
})
.show();
} else {

// Camera permission has not been granted yet. Request it directly.
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA},
REQUEST_CAMERA);
}
}

/**
* Display the {@link CameraPreviewFragment} in the content area if the required Camera
* permission has been granted.
*/
private void showCameraPreview() {
getSupportFragmentManager().beginTransaction()
.replace(R.id.sample_content_fragment, CameraPreviewFragment.newInstance())
.addToBackStack("contacts")
.commit();
}

/**
* Callback received when a permissions request has been completed.
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {

if (requestCode == REQUEST_CAMERA) {

// Received permission result for camera permission.est.");
// Check if the only required permission has been granted
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Camera permission has been granted, preview can be displayed
Snackbar.make(mLayout, R.string.permision_available_camera,
Snackbar.LENGTH_SHORT).show();
} else {
Snackbar.make(mLayout, R.string.permissions_not_granted,
Snackbar.LENGTH_SHORT).show();

}
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLayout = findViewById(R.id.sample_main_layout);

if (savedInstanceState == null) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
RuntimePermissionsFragment fragment = new RuntimePermissionsFragment();
transaction.replace(R.id.sample_content_fragment, fragment);
transaction.commit();
}
}
}

关于Android 6.0 权限错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33078003/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com