当前位置: 首页 > 知识库问答 >
问题:

来自应用程序的运行时权限

宋飞文
2023-03-14
 @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();
        }
    }
}
@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 313 59-31359/com.gic.spade.android e/AndroidRuntime:致命异常:main process: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(未知源:0),在Android.app.activitythread.$h.handleMessage(activitythread.java:1764),在Android.os.handler.dispatchMessage(handler.java:105),在Android.os.handler.lappedmessage(handler.java:164),在Android.app.activitythread.main在com.android.internal.os.zygoteinit.main(zygoteinit.java:1374)处运行(zygoteinit: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.itelephony:stub$proxy.getDeviceId(itelephony.java:5333),在Android.telephony.telephonymanager.getDeviceId(telephonymanager.java:1069),在com.gic.spade.android.logic.propertyManager。.instrumentation.callapplicationoncreate(instrumentation.java:1125)在Android.app.activitythread.handleBindApplication(activitythread.java:6062)在Android.app.activitythread.-Wrap1(未知来源: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.invoke(本机方法) 在com.android.internal.os.zygote$methodandargscaller.run(zygote.java:327) 在com.android.internal.os.zygoteinit.main(zygoteinit.java:1374) 

共有1个答案

单勇
2023-03-14

如果您甚至在请求权限之前就需要权限,也许您应该得出这样的结论:您的代码没有很好的体系结构。

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

在您的问题中,您似乎希望在创建应用程序时发送设备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
}
 类似资料:
  • 这是StackTrace: 上下文初始化失败org.springframework.beans.factory.beanCreationException:创建ServletContext资源[/WEB-INF/applicationContext.xml]中定义的名为“SQL SessionFactory”的bean时出错:bean初始化失败;嵌套异常是java.lang.reflect.Mal

  • 问题内容: 在使用mocha和supertest对我的node.js应用程序(基本上是REST后端)进行单元测试时,我只需要屏幕上特定于测试的消息,但标准输出也被应用程序日志消息所困扰。 我从以下单元测试开始: …并获得此输出(这是 不 应该的): 我用[App]标记了应用程序日志消息。我 真正想要的 是单元测试的输出: 我如何抑制应用程序插入Mocha的报告程序输出的console.log /

  • 例如。 这应该运行一个简单的hello world应用程序,摘自oracle文档。然而,当我‘运行’这个代码,没有窗口打开。取而代之的是打开一个名为“Java”的应用程序。看起来'java'只是一个位于'jdk1.8.0_25.jdk/contents/home/bin'中的'UNIX可执行文件‘。应用程序'java'绝对不显示任何东西,并且在没有强制退出的情况下无法关闭。 我在MacBook上运

  • 我已在清单文件中添加了所有权限。在Lollipop中,所有应用程序都会在安装应用程序期间询问权限,但当应用程序安装在Marshmallow中时,权限不是询问。 清单权限是

  • 我试图从Java代码运行chimpchat,但我得到了以下错误: 意外异常“无法运行程序”/home/asco/adt-bundle-linux-x86_64/sdk/platform-tools“:错误=13,试图从”/home/asco/adt-bundle-linux-x86_64/sdk/platform-tools“获取adb版本时拒绝”权限“ 我可以以普通用户的身份从shell运行ad