运行 应用程序的时间许可

Run time permission from application

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

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

但不是打开初始屏幕 activity,创建方法覆盖了 Collect.java 应用程序 class 的创建方法,代码是。

@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: FATAL EXCEPTION: main Process: com.gic.spade.android, PID: 31359 java.lang.RuntimeException: Unable to create application com.gic.spade.android.application.Collect: java.lang.SecurityException: getDeviceId: Neither user 10366 nor current process has android.permission.READ_PHONE_STATE. at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6065) at android.app.ActivityThread.-wrap1(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1764) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6944) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374) Caused by: java.lang.SecurityException: getDeviceId: Neither user 10366 nor current process has android.permission.READ_PHONE_STATE. at android.os.Parcel.readException(Parcel.java:1959) at android.os.Parcel.readException(Parcel.java:1905) at com.android.internal.telephony.ITelephony$Stub$Proxy.getDeviceId(ITelephony.java:5333) at android.telephony.TelephonyManager.getDeviceId(TelephonyManager.java:1069) at com.gic.spade.android.logic.PropertyManager.(PropertyManager.java:135) at com.gic.spade.android.application.Collect.onCreate(Collect.java:260) at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1125) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6062) at android.app.ActivityThread.-wrap1(Unknown Source:0)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1764)  at android.os.Handler.dispatchMessage(Handler.java:105)  at android.os.Looper.loop(Looper.java:164)  at android.app.ActivityThread.main(ActivityThread.java:6944)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374) 

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

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

在您的问题中,您似乎希望在创建应用程序时发送设备 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
}