gpt4 book ai didi

Android studio 请求权限和 IMEI/MEID

转载 作者:行者123 更新时间:2023-11-29 01:03:18 25 4
gpt4 key购买 nike

我在权限请求中遇到问题,当我运行我的应用程序时我得到 this message .当我点击“给我许可”时,它会打开 toast 并询问我是允许还是拒绝,当我点击允许时,它会关闭应用程序。然后我应该再次打开它,它会正常工作。当我单击按钮时,会显示我允许权限的 toast 消息,我的代码是否有任何错误?
谢谢

在 list 中:

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

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

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 studio 请求权限和 IMEI/MEID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49392284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com