Android studio请求权限和IMEI / MEID

时间:2018-03-20 19:02:43

标签: android

我在权限请求中遇到问题,当我运行我的应用程序时,我得到this message。当我点击"给我许可"它打开烤面包并要求我允许或拒绝,当我点击允许它将关闭应用程序。然后我应该再次打开它,它将正常工作。当我点击按钮时,有一个允许许可的Toast消息,我的代码中是否有任何错误?
谢谢

IN MANIFEST:

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

在onclick()函数的java代码中,名称为onSubmit():

public class MainActivity extends AppCompatActivity {
  private int PHONE_STATE_CODE=1;

  private Button button;
  TextView textView;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_main );

    textView = (TextView) findViewById( R.id.textView2 );
  }

  public void onSubmit(View v){
    TelephonyManager t = (TelephonyManager) getSystemService( 
    Context.TELEPHONY_SERVICE );
    if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
      Toast.makeText(MainActivity.this, "you have already granted", Toast.LENGTH_SHORT).show();
    } else {
      reqPhonePermission();
    }

    String me =t.getDeviceId();

    String id = t.getLine1Number();
  }

~~~~~~~ 在onClick()

之外
private void reqPhonePermission(){
    if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_PHONE_STATE)){
        new AlertDialog.Builder(this).setTitle("permission needed").setMessage("this permission needed")
                .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.READ_PHONE_STATE},PHONE_STATE_CODE);

                    }
                }) .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        }).create().show();
    } else {
        ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_PHONE_STATE},PHONE_STATE_CODE);
    }
}

public void onRequestPermissionResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults ){
    if(requestCode == PHONE_STATE_CODE){
        if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
            Toast.makeText(this,"Permission granted",Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this,"Permission denied",Toast.LENGTH_SHORT).show();
        }
    }

1 个答案:

答案 0 :(得分:1)

只有在授予权限的情况下才应调用需要权限的方法 当其中一个条件成立时,您可以确定授予权限:

  • ContextCompat.checkSelfPermission == PackageManager.PERMISSION_GRANTED

  • grantResults[0] == PackageManager.PERMISSION_GRANTED

尝试使用此代码:

public void onSubmit(View v){
    TelephonyManager t = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE );
    if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
        Toast.makeText(MainActivity.this, "you have already granted", Toast.LENGTH_SHORT).show();
        methodThatRequiresPermission();
    } else {
        reqPhonePermission();
    }
}

public void onRequestPermissionResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults ){
    if(requestCode == PHONE_STATE_CODE){
        if(grantResults.length > 0 && grantResults[0] ==  PackageManager.PERMISSION_GRANTED){
            Toast.makeText(this,"Permission granted",Toast.LENGTH_SHORT).show();
            methodThatRequiresPermission();
        } else {
            Toast.makeText(this,"Permission denied",Toast.LENGTH_SHORT).show();
        }
    }
}

private void methodThatRequiresPermission() { 
    String me =t.getDeviceId();
    String id = t.getLine1Number();

    //Use the values here
}