我的权限请求有问题,当我运行我的应用程序时,我得到了this message。当我单击“授予我权限”时,它将打开吐司并要求我允许或拒绝,当我单击“允许”时,它将关闭该应用程序。然后,我应该再次打开它,它将正常工作。当我单击按钮时,有一条允许我允许的吐司消息,我的代码是否存在任何错误?
谢谢

在清单中:

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

最佳答案

仅当授予许可时,才应调用需要许可的方法。
您可以确定在满足以下条件之一时就授予了权限:


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
}

关于android - Android Studio请求权限和IMEI/MEID,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49392284/

10-12 03:43
查看更多