Android M permission of the tested READ_PHONE_STATE(dangerous permissions)

前端 未结 2 958
小蘑菇
小蘑菇 2020-12-31 12:37

If the device is running Android 6.0 or higher when im trying to get phone number using getLine1Number():

java.lang.SecurityException: Requires

2条回答
  •  说谎
    说谎 (楼主)
    2020-12-31 13:15

    public class MainActivity extends AppCompatActivity {
    
        TextView textView;
        String device_unique_id,IMEI;
    
    
        private static final int MY_PERMISSIONS_REQUEST_READ_PHONE_STATE = 0;
    
    
       @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            textView = (TextView)findViewById(R.id.textView);
        }
        public void GetImei(View view)
        {
    
    
    
            loadIMEI();
    
        }
    
        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) {
                if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                        Manifest.permission.READ_PHONE_STATE)) {
    //                get_imei_data();
                } else {
                    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE},
                            MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);
                }
            } else {
    
                TelephonyManager mngr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
                IMEI = mngr.getDeviceId();
                device_unique_id = Settings.Secure.getString(this.getContentResolver(),
                        Settings.Secure.ANDROID_ID);
                textView.setText(device_unique_id+"----"+mngr.getDeviceId());
                // READ_PHONE_STATE permission is already been granted.
                Toast.makeText(this,"Alredy granted",Toast.LENGTH_SHORT).show();
            }
        }
           @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,@NonNull int[] grantResults) {
    
            if (requestCode == MY_PERMISSIONS_REQUEST_READ_PHONE_STATE) {
    
                if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    
    //                Toast.makeText(this,"Alredy DONE",Toast.LENGTH_SHORT).show();
                    TelephonyManager mngr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
                    IMEI = mngr.getDeviceId();
                    device_unique_id = Settings.Secure.getString(this.getContentResolver(),Settings.Secure.ANDROID_ID);
                    textView.setText(device_unique_id+"----"+mngr.getDeviceId());
    
                } else {
                    Toast.makeText(this,"ehgehfg",Toast.LENGTH_SHORT).show();
                }
            }
        }
    

提交回复
热议问题