测试READ_PHONE_STATE的Android M权限(危险权限)

时间:2016-02-10 15:47:54

标签: java android mobile permissions

如果设备在使用 getLine1Number()时尝试获取电话号码时运行Android 6.0或更高版本:

java.lang.SecurityException:需要READ_PHONE_STATE:用户10184和当前进程都没有android.permission.READ_PHONE_STATE。 这是出来的。

我宣布许可为:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

2 个答案:

答案 0 :(得分:19)

在Android 6.0中,您需要明确要求用户授予权限。只是在清单中宣布它是不够的。

文档中的article是开始学习新模型的好地方,但我会做一个简短的总结。

每次执行需要&#34;危险许可的操作时,&#34;您需要检查当前是否已授予权限,因为用户可以随时撤消该权限。

可以使用char ** => const char**方法完成此操作。

checkSelfPermission

您可以使用if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) { // We do not have this permission. Let's ask the user } 方法申请权限,

requestPermissions

其中ActivityCompat.requestPermissions(thisActivity, new String[]{Manifest.permission.READ_PHONE_STATE}, PERMISSION_READ_STATE); 是您定义的常量整数,以便稍后检入回调方法。

然后,您将覆盖活动中的PERMISSION_READ_STATE,并查看是否已授予权限。如果是的话,你可以继续进行危险的行动。

onRequestPermissionsResult

答案 1 :(得分:3)

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();
            }
        }
    }