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?
requestPermissions
method requires an activity instead of context, you'll probably have to invoke that method from an activity in aMyApplication.checkAndRequestPermissions(this, ...)
style. – GrefeActivityCompat.requestPermissions((Activity) applicationContext, new String[]{android.Manifest.permission.READ_SMS}, Constants.REQUEST_CODE_READ_SMS);
. not sure but hope it helps. – Carnivore(Activity) applicationContext
doesn't work since there is no activityContext available here. – ThomeyonCreate
where I collect bunch of information before proceeding. – Thomeycontext
is not available then usegetApplicationContext();
. And what is theapplicationContext
in your codePermissionUtility.isPermitted(applicationContext, android.Manifest.permission.READ_SMS)
– CarnivoreapplicationContext
isgetApplicationContext();
– Thomey