Request Permission from Application class [duplicate]

蓝咒 提交于 2021-02-16 16:09:14

问题


As per the new Android doc, In order to collect certain data, I need to request for permission. So I am doing this :

@Override
public void onCreate() {
    super.onCreate();
    ...
    ...
    if (PermissionUtility.isPermitted(applicationContext, android.Manifest.permission.READ_SMS)) {
        userNumber = getUserPhoneNumber();
    } else {
        ActivityCompat.requestPermissions(activity,
                new String[]{android.Manifest.permission.READ_SMS},
                Constants.REQUEST_CODE_READ_SMS);
    }
    ...
    ...
}

The problem is I m doing it in my class which is public class MyApplication extends Application{...} so activity isn't available here. Is there a way to ask for permissions in this class or a way around to pass an activity?


回答1:


There is no way for you request Permission from Application. You always need an activity for it.

When Application called, no Activity is initialized.

You can refer to this answer that why we need an activity for request Permission:

Android: ActivityCompat.requestPermissions requires activity and not context



来源:https://stackoverflow.com/questions/38219056/request-permission-from-application-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!