Java 被测READ_PHONE_STATE的Android M权限(危险权限)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35319702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:32:54  来源:igfitidea点击:

Android M permission of the tested READ_PHONE_STATE(dangerous permissions)

javaandroidmobilepermissions

提问by Sanket Prabhu

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

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

java.lang.SecurityException: Requires READ_PHONE_STATE: Neither user 10184 nor current process has android.permission.READ_PHONE_STATE.This is coming out.

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

I declared permission as :

我宣布许可为:

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

采纳答案by Andrew Brooke

In Android 6.0, you need to explicitly ask the user to grant the permissions. Just declaring it in the manifest isn't enough.

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

This articlein the docs is a great place to start learning the new model, but I'll give a brief summary.

文档中的这篇文章是开始学习新模型的好地方,但我会给出一个简短的总结。

Every time you perform an action that requires a "dangerous permission," you need to check if the permission is currently granted, because the user can revoke it at any time.

每次执行需要“危险权限”的操作时,都需要检查当前是否授予权限,因为用户可以随时撤消它。

This can be done with the checkSelfPermissionmethod.

这可以通过checkSelfPermission方法来完成。

if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.READ_PHONE_STATE)
    != PackageManager.PERMISSION_GRANTED) {
        // We do not have this permission. Let's ask the user
}

You can request the permission with the requestPermissionsmethod, as such

您可以使用该requestPermissions方法请求权限,例如

ActivityCompat.requestPermissions(thisActivity, new String[]{Manifest.permission.READ_PHONE_STATE}, PERMISSION_READ_STATE);

Where PERMISSION_READ_STATEis a constant integer defined by you to check in the callback method later.

PERMISSION_READ_STATE您定义的常量整数在哪里,以便稍后检查回调方法。

You will then override onRequestPermissionsResultin your activity and see if the permission was granted. If it was, you can go ahead and preform the dangerous action.

然后onRequestPermissionsResult,您将覆盖您的活动并查看是否授予了权限。如果是,您可以继续执行危险操作。

@Override
public void onRequestPermissionsResult(int requestCode,
        String permissions[], int[] grantResults) {
    switch (requestCode) {
        case PERMISSION_READ_STATE: {
            if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // permission granted!
                // you may now do the action that requires this permission
            } else {
                // permission denied
            }
            return;
        }

    }
}

回答by Rajasimman

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