Android 6.0权限错误
我得到这个错误:
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)
尽pipe我已经明确表示了。 Android 6.XX棉花糖设备是否有任何更改? 我需要授予READ_PHONE_STATE
获取设备的IMEI权限。 PLS。 帮帮我。
Android M权限已更改权限现在在运行时请求,而不是安装Android M之前的时间。
你可以在这里查看文档
此版本引入了新的权限模型,用户现在可以在运行时直接pipe理应用程序权限。 该模型为用户提供了改进的可视性和对权限的控制,同时简化了应用程序开发人员的安装和自动更新stream程。 用户可以为已安装的应用单独授予或撤销权限。
在您的目标为Android 6.0(API级别23)或更高的应用程序上,请确保在运行时检查并请求权限。 要确定您的应用程序是否已被授予权限,请调用新的checkSelfPermission()方法。 要请求权限,请调用新的requestPermissions()方法。 即使您的应用没有针对Android 6.0(API级别23),也应该在新的权限模式下testing您的应用。
有关在您的应用程序中支持新权限模型的详细信息,请参阅使用系统权限。 有关如何评估对应用程序的影响的提示,请参阅权限最佳做法。
要检查权限,你必须从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(); } } }
我修改了@Modge的代码,在我的情况下,我需要IMEI权限,最好的方法是在运行期间,所以我这样做:
/** * A loading screen after AppIntroActivity. */ public class LoadingActivity extends BaseActivity { private static final int MY_PERMISSIONS_REQUEST_READ_PHONE_STATE = 0; private TextView loading_tv2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_loading); //trigger 'loadIMEI' loadIMEI(); /** Fading Transition Effect */ overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out); } /** * Called when the 'loadIMEI' function is triggered. */ public void loadIMEI() { // Check if the READ_PHONE_STATE permission is already available. if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) { // READ_PHONE_STATE permission has not been granted. requestReadPhoneStatePermission(); } else { // READ_PHONE_STATE permission is already been granted. doPermissionGrantedStuffs(); } } /** * Requests the READ_PHONE_STATE permission. * If the permission has been denied previously, a dialog will prompt the user to grant the * permission, otherwise it is requested directly. */ private void requestReadPhoneStatePermission() { if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_PHONE_STATE)) { // 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. new AlertDialog.Builder(LoadingActivity.this) .setTitle("Permission Request") .setMessage(getString(R.string.permission_read_phone_state_rationale)) .setCancelable(false) .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { //re-request ActivityCompat.requestPermissions(LoadingActivity.this, new String[]{Manifest.permission.READ_PHONE_STATE}, MY_PERMISSIONS_REQUEST_READ_PHONE_STATE); } }) .setIcon(R.drawable.onlinlinew_warning_sign) .show(); } else { // READ_PHONE_STATE permission has not been granted yet. Request it directly. ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, MY_PERMISSIONS_REQUEST_READ_PHONE_STATE); } } /** * Callback received when a permissions request has been completed. */ @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == MY_PERMISSIONS_REQUEST_READ_PHONE_STATE) { // Received permission result for READ_PHONE_STATE permission.est."); // Check if the only required permission has been granted if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // READ_PHONE_STATE permission has been granted, proceed with displaying IMEI Number //alertAlert(getString(R.string.permision_available_read_phone_state)); doPermissionGrantedStuffs(); } else { alertAlert(getString(R.string.permissions_not_granted_read_phone_state)); } } } private void alertAlert(String msg) { new AlertDialog.Builder(LoadingActivity.this) .setTitle("Permission Request") .setMessage(msg) .setCancelable(false) .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // do somthing here } }) .setIcon(R.drawable.onlinlinew_warning_sign) .show(); } public void doPermissionGrantedStuffs() { //Have an object of TelephonyManager TelephonyManager tm =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); //Get IMEI Number of Phone //////////////// for this example i only need the IMEI String IMEINumber=tm.getDeviceId(); /************************************************ * ********************************************** * This is just an icing on the cake * the following are other children of TELEPHONY_SERVICE * //Get Subscriber ID String subscriberID=tm.getDeviceId(); //Get SIM Serial Number String SIMSerialNumber=tm.getSimSerialNumber(); //Get Network Country ISO Code String networkCountryISO=tm.getNetworkCountryIso(); //Get SIM Country ISO Code String SIMCountryISO=tm.getSimCountryIso(); //Get the device software version String softwareVersion=tm.getDeviceSoftwareVersion() //Get the Voice mail number String voiceMailNumber=tm.getVoiceMailNumber(); //Get the Phone Type CDMA/GSM/NONE int phoneType=tm.getPhoneType(); switch (phoneType) { case (TelephonyManager.PHONE_TYPE_CDMA): // your code break; case (TelephonyManager.PHONE_TYPE_GSM) // your code break; case (TelephonyManager.PHONE_TYPE_NONE): // your code break; } //Find whether the Phone is in Roaming, returns true if in roaming boolean isRoaming=tm.isNetworkRoaming(); if(isRoaming) phoneDetails+="\nIs In Roaming : "+"YES"; else phoneDetails+="\nIs In Roaming : "+"NO"; //Get the SIM state int SIMState=tm.getSimState(); switch(SIMState) { case TelephonyManager.SIM_STATE_ABSENT : // your code break; case TelephonyManager.SIM_STATE_NETWORK_LOCKED : // your code break; case TelephonyManager.SIM_STATE_PIN_REQUIRED : // your code break; case TelephonyManager.SIM_STATE_PUK_REQUIRED : // your code break; case TelephonyManager.SIM_STATE_READY : // your code break; case TelephonyManager.SIM_STATE_UNKNOWN : // your code break; } */ // Now read the desired content to a textview. loading_tv2 = (TextView) findViewById(R.id.loading_tv2); loading_tv2.setText(IMEINumber); } }
希望这可以帮助你或某人。
我做了这样的事情:
if (checkSelfPermission(Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) { requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE}, PERMISSIONS_REQUEST_READ_PHONE_STATE); } else { setDeviceImei(); } } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { if (requestCode == PERMISSIONS_REQUEST_READ_PHONE_STATE && grantResults[0] == PackageManager.PERMISSION_GRANTED) { setDeviceImei(); } } private void setDeviceImei() { mTelephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); //Code ......... .............. }
我将如何获得我需要在运行时设置的权限列表..?
我们可以从https://developer.android.com/guide/topics/permissions/requesting.htmlfind所有危险的权限;
但是我觉得在运行时检查和请求危险的权限有点烦人。所以我只是用TARGET_SDK_VERSION = 22做一个懒人(╰_╯)