2015-10-12 24 views
18

Tôi nhận được lỗi này:Android 6.0 Permission Lỗi

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) 

tôi đã cho nó trong manifest mặc dù. Có bất kỳ thay đổi nào liên quan đến Android 6.XX Marshmallow thiết bị không? Tôi cần cấp cho số READ_PHONE_STATE quyền nhận IMEI của thiết bị. Xin lỗi. Cứu giúp.

+0

Hãy thử cách này có thể giúp bạn: -http: //stackoverflow.com/a/41221852/5488468 –

Trả lời

28

quyền Yes đã thay đổi trên Android M. Quyền hiện đang yêu cầu trong thời gian chạy như trái ngược với thời gian cài đặt trước để Android M.

Bạn có thể kiểm tra các tài liệu 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.

Để kiểm tra các điều khoản bạn phải kiểm tra như thế này, được lấy từ 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(); 
     } 
    } 
} 
+2

Câu trả lời là hoàn hảo. Tôi cũng muốn thêm một liên kết tới blog, nơi nó được giải thích "http://inthecheesefactory.com/blog/things-you-need-to-know-about-android-m-permission-developer-edition/en" – SHS

+1

@SHS chỉnh sửa url blog của bạn và xóa "ký tự cuối cùng" cảm ơn –

+0

Thankx @SayedAbolfazlFatemi vì đã chỉ ra lỗi. Liên kết blog được sửa lỗi là - http://inthecheesefactory.com/blog/things-you-need-to-know- about-android-m-permission-developer-edition/vi – SHS

6

Tôi đã làm một cái gì đó như thế này:

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 
......... 
.............. 

    } 

Làm cách nào tôi sẽ nhận được Danh sách giấy phép mà tôi cần đặt trong thời gian chạy ..?

+0

Tôi nhận được lỗi: NoSuchMethodError: trên checkSelfPermission, tôi có thể xử lý nó bằng cách kiểm tra phiên bản xây dựng nhưng tôi sẽ không phải là một giải pháp tổng quát, là nó thực sự cần thiết để làm điều đó như nếu vào sản xuất nó sẽ là một giải pháp trợ giúp băng cho marshmellow. –

11

tôi sửa đổi @ đang Modge của, trong trường hợp của tôi, tôi cần phải xin phép IMEI và cách tốt nhất để đi là trong thời gian chạy vì vậy tôi đã làm điều này:

/** 
* 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); 
} 
} 

Hy vọng điều này sẽ giúp bạn hoặc ai đó.

0

Chúng tôi có thể tìm thấy tất cả các quyền gây nguy hiểm từ https://developer.android.com/guide/topics/permissions/requesting.html

Tuy nhiên, tôi nghĩ rằng đó là một chút khó chịu để kiểm tra và yêu cầu quyền tại nguy hiểm runtime.So tôi chỉ làm với TARGET_SDK_VERSION = 22, một anh chàng lười biếng (╰_╯)

Các vấn đề liên quan