Request permission for Location updates inside IntentService - java

How do I request permission for my location (api 23>) when I want to access the location inside a service?
I have a service running every minute fetching data from a URL. In this service I want to pass my location in the URL but I have issues requesting the permission for my location (since you cant do that inside a service). How can I request permission in my MainActivity?
EDIT: I think my problem is requesting permissions inside a service. My service is started with alarmManager (setRepeating), and in this service I want to access my real time location. When I try to request permission there it does not work. Can I request permissions elsewhere?

Update: If the app starts for the first time, you can ask the user for permission (from your Activity) - user can grant or deny the permission.
If user has denied the permission you should stop/don't start your service and inform the user that, without this permission, your app doesn't work.
See Requesting Permissions at Run Time.

Related

How do I grant SYSTEM_ALERT_WINDOW permission automatically on request, in my android App?

For context, the Android documentation says that some app classes receive the SYSTEM_ALERT_WINDOW permission automatically on request:
Any app that has ROLE_CALL_SCREENING and requests SYSTEM_ALERT_WINDOW automatically receives the permission. If the app misses ROLE_CALL_SCREENING, it loses permission.
Any app that is capturing the screen using a MediaProjection and requests SYSTEM_ALERT_WINDOW automatically receives permission, unless explicitly denied by the user. When the app stops capturing the screen, it loses permission. This use case is primarily intended for apps to stream games live.
How can I achieve this configuration to be able to obtain this permission automatically or by default?
As the documentation you quoted says, if you don't fit into one of those use cases, you can't.
It needs to be explicitly granted, and not all devices even support it.
This is because being able to draw over the screen whenever the app wants opens the door to bad behaviors, such as disguising other parts of the UI or preventing the using from using the device properly.

Changing application permission causes the oncreate() called again

I am working on run time permissions. When I am in my activity class while creating a report having images, when permission is changed app crashed and I lost my data.I have used OnsaveInstance but it does not do what I really want. I want to restart my app when someone change app permission from settings and save my report.
which method of the activity is called when permission changed?.
How i know that permission is changed from background?
You can check permission in your activity startup or resume, like
this answer
and for restarting app you can try any answer from this Q/ans thread, one of the accepted answer for restarting activity here
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage( getBaseContext().getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
How i know that permission is changed from background.?
If, from Settings, the user removes a permission that the user granted previously, your process is terminated immediately. You find out that the permission changed the next time your app runs.
If, from Settings, the user grants a permission that the user had not granted previously, then your process is left alone, but you also are not informed about the new permission. You are welcome to call checkSelfPermission() at appropriate points to see if you now have the permission that you lacked before.
To check if you currently have a permission you use ContextCompat.checkSelfPermission(), see the developer docs for details.
If you need the permission in a background thread then you check if you have the permission before you do the work. If you do not have the permission you will have to skip the work, if you just continue then your code will most likely throw a SecurityException.
How you handle it when you don't have the required permissions depends on the use case. You could create a system notification that informs the user the background task could not be performed due to a missing permission. If the user taps the notification then you can request the permission from the user before retrying the task. See the same developer docs for how to request a permission from the user.
If it would be okay to abort the background task then you could do just that but you will probably want to show somewhere in your user interface that the background task needs a permission that is missing and ask the user to grant it so the (optional) background task can do its work.

Android BroadcastRecevier for App Permissions

How would you create a broadcast receiver for when there is a change in your app's permissions?
You cannot create a BroadcastReceiver for App permissions since there is no broadcast. As soon as there is a permission change, the app is restarted.
EDIT: You can check for permissions yourselves by following this.

When exactly to ask for Runtime Permission?

Since Runtime Permissions were introduced in Android Marshmallow, I've been meaning to apply the new way for requesting permissions and I've read and understood the dangerous permissions and how to check for them and request them.
The question is, If I have an Activity that contains a Button for opening Camera.
Do I check and request the permission when the Activity is opened? Or when the button is clicked?
Please explain why your suggested way is preferred.
Do I check and request the permission when the Activity is opened? Or when the button is clicked?
If the only purpose of the Activity is to take a picture when the button is clicked, I would request the permission when the activity is opened.
If the Activity has other roles, such that the take-a-picture button is only one feature out of many, I would ask for the permission when the user clicks the button.
In other words, once the user has requested to go down a path that will (almost) certainly need the permission, ask for the permission.
According to me, when click on button ask for permission to open camera.
because activity can have many component which can request for like camera, contacts, write storage, etc
so you must not ask for permission when activity opens but when you click on button ask for camera permission
if you want to send SMS than ask when you need to send not at opening of activity.
Permissions Best Practices.
You can download official sample app here.
hope you get it.
Starting from Android SDK 23, system permissions are divided into two categories, normal and dangerous:
Normal permissions do not directly risk the user's privacy. If your
app lists a normal permission in its manifest, the system grants the
permission automatically.
Dangerous permissions can give the app access to the user's
confidential data.
If your app lists a normal permission in its manifest, the system grants the permission automatically. If you list a dangerous permission, the user has to explicitly give approval to your app.
For more information, see Normal and Dangerous Permissions.
If your app targets API 23, and you need to ask user for a dangerous
permission (such as READ_CONTACT or READ_CALL_LOG etc.), you need to
ask for permission in runtime.

Android Authentication Mananger, when is user permission required?

I am writing an Android App and I need to request an Auth token from the account. Getting the list of accounts and the token is easy, I have that working so far.
The problem is that Jelly Bean opens a popup, asking for users permission. Now my question(s):
What are the rules for the popup dialog to appear? I had that happen once, for the first time after I updated my device to Android 4.1. It seem to happen randomly: installing and uninstalling the App I was not be able to reproduce the situation where this popup comes back.
Is there any way to reset it without doing factory reset? I need to reproduce this for testing for question 3.)
Is there a way to find out if the popup will appear before requesting the authtoken and i.e. wait until it's not required anymore (i.e, if user has the app in question installed but never used or if authtoken is expired?) and request it then?
I know that most Android users are very inexperienced and may get scared by some permission requests and I'd like to avoid requesting authtoken when it would trigger this user popup.
I Tried to revoke acces to an app engine project of mine and I was able to revoke the permission by going to google accounts setting page. There you can revoke access from android login service. After that I had to remove the google account from my phone and add it back. If you start your app again you should see the permission dialog again.
Resetting/revoking the permission generally depend on the type of token. There are two sides of this: revoking the token on the server side using your Google profile page and revoking the permission for your app to use this type of token (on Android). For the server side you can revoke OAuth and AuthSub tokens, but not ClientLogin tokens. Go to https://www.google.com/settings/security and click the 'Edit' button in the 'Authorizing applications and sites' section. For revoking the on device permission, uninstalling the your app should reset it (it removes an entry in a system database when your package is uninstalled). Apparently this doesn't work properly on some devices, but it should work. The dialog will only appear when you try to use a token, so it shouldn't appear before you request it. You should handle expiry in your app (i.e., request a fresh token), but this won't show a permission dialog.

Categories