0

我想请求应用程序类的许可。如何请求访问设备 ID 的权限?我无法请求活动的许可,因为它在启动初始屏幕活动之前崩溃。以下代码是初始屏幕活动的一部分。

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    checkpermi();

检查权限的代码

public  void checkpermi() {    
if(ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
            + ContextCompat.checkSelfPermission(
            this,Manifest.permission.READ_PHONE_STATE)
            + ContextCompat.checkSelfPermission(
            this,Manifest.permission.WRITE_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED){

        // Do something, when permissions not granted
        if(ActivityCompat.shouldShowRequestPermissionRationale(
                this,Manifest.permission.ACCESS_FINE_LOCATION)
                || ActivityCompat.shouldShowRequestPermissionRationale(
                this,Manifest.permission.READ_PHONE_STATE)
                || ActivityCompat.shouldShowRequestPermissionRationale(
                this,Manifest.permission.WRITE_EXTERNAL_STORAGE)){
            // If we should give explanation of requested permissions

            // Show an alert dialog here with request explanation
            AlertDialog.Builder builder = new AlertDialog.Builder(SplashScreenActivity.this);
            builder.setMessage("Location, Read Contacts and Write External" +
                    " Storage permissions are required to do the task.");
            builder.setTitle("Please grant those permissions");
            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    ActivityCompat.requestPermissions(
                            SplashScreenActivity.this,
                            new String[]{
                                    Manifest.permission.ACCESS_FINE_LOCATION,
                                    Manifest.permission.READ_PHONE_STATE,
                                    Manifest.permission.WRITE_EXTERNAL_STORAGE
                            },

                            STORAGE_PERMISSION_CODE
                    );
                }
            });
            builder.setNeutralButton("Cancel",null);
            AlertDialog dialog = builder.create();
            dialog.show();
        }else{
            // Directly request for required permissions, without explanation
            ActivityCompat.requestPermissions(
                    this,
                    new String[]{
                            Manifest.permission.ACCESS_FINE_LOCATION,
                            Manifest.permission.READ_PHONE_STATE,
                            Manifest.permission.WRITE_EXTERNAL_STORAGE
                    },
                    STORAGE_PERMISSION_CODE
            );
        }
    }else {
        // Do something, when permissions are already granted
        Toast.makeText(SplashScreenActivity.this,"Permissions already granted",Toast.LENGTH_SHORT).show();
    }
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

    //Checking the request code of our request
    if(requestCode == STORAGE_PERMISSION_CODE){

        //If permission is granted
        if(grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){

            //Displaying a toast
            Toast.makeText(this,"Permission granted now you can read the storage",Toast.LENGTH_LONG).show();
        }else{
            //Displaying another toast if permission is not granted
            Toast.makeText(this,"Oops you just denied the permission",Toast.LENGTH_LONG).show();
        }
    }
}

但不是打开启动屏幕活动,而是使用 Collect.java 应用程序类的 on create 方法覆盖 on create 方法,并且代码是。

@Override
public void onCreate() {
    singleton = this;

    super.onCreate();

    PropertyManager mgr = new PropertyManager(this);


    mActivityLogger = new ActivityLogger(

            mgr.getSingularProperty(PropertyManager.DEVICE_ID_PROPERTY));
}

如果我无法删除上面的代码,因为应用程序内的大多数按钮都不起作用。

错误日志是。

2020-02-27 16:36:12.159 31359-31359/com.gic.spade.android E/AndroidRuntime:致命异常:主进程:com.gic.spade.android,PID:31359 java.lang.RuntimeException:无法创建应用程序 com.gic.spade.android.application.Collect:java.lang.SecurityException:getDeviceId:用户 10366 和当前进程都没有 android.permission.READ_PHONE_STATE。在 android.app.ActivityThread.handleBindApplication(ActivityThread.java:6065) 在 android.app.ActivityThread.-wrap1(Unknown Source:0) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1764) 在 android。 os.Handler.dispatchMessage(Handler.java:105) 在 android.os.Looper.loop(Looper.java:164) 在 android.app.ActivityThread.main(ActivityThread.java:6944) 在 java.lang.reflect.Method . 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374) 的 com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) 调用(本机方法) 原因:java。 lang.SecurityException:getDeviceId:用户 10366 和当前进程都没有 android.permission.READ_PHONE_STATE。在 android.os.Parcel.readException(Parcel.java:1959) 在 android.os.Parcel.readException(Parcel.java:1905) 在 com.android.internal.telephony.ITelephony$Stub$Proxy.getDeviceId(ITelephony.java :5333) 在 com.gic.spade.android.logic.PropertyManager.(PropertyManager.java:135) 在 com.gic.spade.android.application.Collect 的 android.telephony.TelephonyManager.getDeviceId(TelephonyManager.java:1069) .onCreate(Collect.java:260) 在 android.app.Instrumentation。 

4

1 回答 1

1

如果您甚至在请求许可之前就需要许可,也许您应该得出结论,您的代码架构不好。

只需绕过该代码,直到您可以在授予权限时重新启动它。

在您的问题中,您似乎想在创建应用程序时发送设备 ID。除非您获得许可,否则不要发送它。

if (checkPermission(...) == GRANTED) {
    PropertyManager mgr = new PropertyManager(this);
    mActivityLogger = new ActivityLogger(mgr.getSingularProperty(PropertyManager.DEVICE_ID_PROPERTY));
} else {
    mActivityLogger = new ActivityLogger();
    // Or even disable that logger altogether
}
于 2020-02-27T09:46:44.663 回答