gpt4 book ai didi

Android READ PHONE STATE 权限

转载 作者:行者123 更新时间:2023-11-29 20:03:16 27 4
gpt4 key购买 nike

我正在开发一个需要 READ_PHONE_STATE 权限的小应用程序。我在我的 manifest.xml 上添加了权限请求

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mobile.stoq.stoqpay">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.INTERNET" />
<application
...

然而,在阅读它时,我发现我必须向用户发出实际请求才能获得该权限,因此我尝试使用以下代码 fragment 来做到这一点

public class ValidationActivity extends AppCompatActivity {

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_validation);
//TODO: Fix this permission request. It sort of works but not really
int REQUEST_RPS = 0;
if (ContextCompat.checkSelfPermission(ValidationActivity.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(ValidationActivity.this, new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_RPS);
}

List<UserModel> listOfUser = StoneStart.init(this);

if (listOfUser == null) {

List<String> stoneCodeToActiveList = new ArrayList<>();
String STONE_PRODUCTION_KEY = "846873720";
stoneCodeToActiveList.add(STONE_PRODUCTION_KEY);

ActiveApplicationProvider activeApplicationProvider = new ActiveApplicationProvider(this, stoneCodeToActiveList);
activeApplicationProvider.setDialogMessage("Activating");
activeApplicationProvider.setDialogTitle("Please wait");
activeApplicationProvider.setWorkInBackground(false);
activeApplicationProvider.setConnectionCallback(new StoneCallbackInterface() {
public void onSuccess() {
Toast.makeText(getApplicationContext(), "Activation successful", Toast.LENGTH_SHORT).show();
continueApplication();
}

public void onError() {
Toast.makeText(getApplicationContext(), "Activation failed", Toast.LENGTH_SHORT).show();
}
});
activeApplicationProvider.execute();
} else {
continueApplication();

}
}

private void continueApplication() {

int SPLASH_DISPLAY_LENGTH = 3500;
new Handler().postDelayed(new Runnable() {
public void run() {

Intent mainIntent = new Intent(ValidationActivity.this, MainActivity.class);
ValidationActivity.this.startActivity(mainIntent);
ValidationActivity.this.finish();
}
}, SPLASH_DISPLAY_LENGTH);
}
}

当我的应用程序运行时,它会崩溃或卡住,而在我重新启动它后,我拥有正确的权限并且它运行良好。这是 logcat 向我显示的内容:

03-13 19:49:57.938 6682-6714/com.mobile.stoq.stoqpay W/System.err: java.lang.SecurityException: getDeviceId: Neither user 10068 nor current process has android.permission.READ_PHONE_STATE.
03-13 19:49:57.954 6682-6714/com.mobile.stoq.stoqpay W/System.err: at android.os.Parcel.readException(Parcel.java:1620)
03-13 19:49:57.954 6682-6714/com.mobile.stoq.stoqpay W/System.err: at android.os.Parcel.readException(Parcel.java:1573)
03-13 19:49:57.954 6682-6714/com.mobile.stoq.stoqpay W/System.err: at com.android.internal.telephony.ITelephony$Stub$Proxy.getDeviceId(ITelephony.java:4207)
03-13 19:49:57.954 6682-6714/com.mobile.stoq.stoqpay W/System.err: at android.telephony.TelephonyManager.getDeviceId(TelephonyManager.java:706)
03-13 19:49:57.954 6682-6714/com.mobile.stoq.stoqpay W/System.err: at stone.providers.ActiveApplicationProvider.doInBackground(ActiveApplicationProvider.java:102)
03-13 19:49:57.954 6682-6714/com.mobile.stoq.stoqpay W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:295)
03-13 19:49:57.954 6682-6714/com.mobile.stoq.stoqpay W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
03-13 19:49:57.954 6682-6714/com.mobile.stoq.stoqpay W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
03-13 19:49:57.954 6682-6714/com.mobile.stoq.stoqpay W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
03-13 19:49:57.954 6682-6714/com.mobile.stoq.stoqpay W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
03-13 19:49:57.954 6682-6714/com.mobile.stoq.stoqpay W/System.err: at java.lang.Thread.run(Thread.java:818)

我做错了什么?我该如何解决这个问题?

最佳答案

ActivityCompat.requestPermissions() 是异步的。它会立即将控制权返回给您。 您还没有权限

ActivityCompat.requestPermissions() 调用之后出现的所有内容移动到一个单独的方法中(我将在此处称为 foo())。然后:

  • 如果 checkSelfPermission() 显示您有权限,调用 foo()

  • 如果 checkSelfPermission() 显示您没有权限,在您的 onRequestPermissionsResult() 方法中,如果您现在有权限,请调用 foo()

  • 如果在onRequestPermissionsResult()中,您仍然没有权限(例如,用户拒绝了您的请求),请以某种方式处理

关于Android READ PHONE STATE 权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35976819/

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