I use AccountManager.getAuthToken from my service to have access to Googledocs and Spreadsheet API.
As I'm doing it from background and don't want to interrupt user , I allow accountManager to raise notification when user interaction needed (using parameter boolean notifyAuthFailure).
As I expected, CallBack is called with AccounManagerFuture,which resolves to Bundle with KEY_INTENT field in such case.
"In that case, you may need to wait
until the user responds, which could
take hours or days or forever. When
the user does respond and supply a new
password, the account manager will
broadcast the
LOGIN_ACCOUNTS_CHANGED_ACTION Intent,
which applications can use to try
again"
It works well, when thing is only about a wrong password. But I run in a problem,that AccountAuthenticator asks for permision at runtime and intent (from notification bar) starts GrantCredentialsPermissionActivity, which after user presses button (Allow or Deny) do not allow me get know that user already responded.
The question is how to get know when interection with user is finished and try again to get AuthToken.
I cant't get onActivityResult from GrantCredentialsPermissionActivity because I start it from NotificationBar (not from my other activity, as I use service while trying to get token)
I can't specify permissions in manifest beforehead, because as i understand it's some custome permissions, which defined by google authenticator.
It doesn't seem to broadcast any intent when user approve/deny requested permission for application at run-time after closing GrantCredentialsPermissionActivity.
Thank you.
Just check again in onResume that will get called even if the permission activity is a dialog. You will be asking more than needed but not excessively.
What I think would be the best way to know when user interaction is finished is to BroadCast an event and receive it.
Related
I am using Android Studio as a development platform for Android App Development.
I have implemented a module of OTP screen for OTP verfication.
I want to achieve following things:
The OTP screen should be consistent even if the user presses back or home screen like PAYTM & app should run in background untill the OTP is not confirmed.
The OTP section should automatically read the incoming message so.
I also want to get the permissions how to set that permission thing
like permission to read sms, permission to read contact which will
either ask allow or deny.
I am new to this thing and I have no idea how to implement this.
1) Please read about Intent Services, which work in the background thread.
https://developer.android.com/training/run-background-service/create-service.html
So you can override the onBackPressed() method in your activity, depends on it(which is connected with Broadcast Receivers)
2) Read about Broadcast Receivers. http://www.vogella.com/tutorials/AndroidBroadcastReceiver/article.html
3) If it comes to permissions in Android API > 23, you need to add them to manifest and also ask at runtime. You can see documentation: https://developer.android.com/training/permissions/requesting.html
I am implementing a service that uses LocationManager to get and utilize the tablet location. This service is start and stop from an activity.
The latest Android requires that permissions are requested on runtime. Now I have managed to do this on an activity by using requestPermission in onCreate , checkSelfPermission everytime I use some Location manager function, and adding the requestPermission function and overriding the onRequestPermissionResult.
It works great.
Now for my service I need to do the same, but these functions seems to work only for activities. How can I activate permissions in a Service?
just in case, I have already asked for permissions in the activity that starts and stops the services
How can I activate permissions in a Service?
You don't. You activate (i.e., request) permissions from an activity. That is not negotiable.
Ideally, you request permissions before the activity starts the service or does something that will eventually cause the service to start (e.g., schedules the job with JobScheduler).
If you determine that your service no longer has the necessary permissions — perhaps the user revoked them from Settings — you could raise a Notification that leads the user to an activity where you re-request the permissions.
It is technically possible for a service to start an activity which requests the permissions. Usually, this is not a good idea, as you may not know what the user is doing at that moment, and the user may be unhappy to have you interrupt them with this permission request.
How can I activate permissions in a Service?
You can't request for permissions from services. Permissions should be asked explicitly which should be visible to the user in UI. However you can ask permission from activity and, if succeed, you can access those resources until user again turned off permission for your app.
how can you "transfer" these permissions to the service?
Permission is assigned for the entire app, so you don't need to transfer it from one activity to another or from one activity to service. Once you get a permission in an Activity, that permission is assigned to the entire app and your services can access the resources then after. cheers :)
In Android Marshmallow, permissions should be requested at runtime when they are needed, instead of all at once when an app is installed. However, I can only seem to request permissions from an Activity, which is a problem since my app contains only Services. (Why is that, you might ask? The app has an Android Wear watch face bundled inside, and all the phone does is look up photos nearby to send to the watch - no Activity needed. But it does require location permissions.)
So, is there any way to request permissions from a Service? Or somehow force the permissions to be granted at install time as in the past?
requestPermission() can only be called from an Activity and not a Service (unlike checkPermission() that only requires PackageManager). So you need to do some extra work to get around that; you do need to provide an Activity in your app and, for example, your Service can check for permissions it needs and if they have not been granted yet, it can create a notification and that can inform user with a descriptive short message as to why there is a notification and what needs to happen when they click on the notification, etc.
I agree, this is very troublesome for services, I think you should report an issue on Android Developer Preview page for this.
At the moment, I think the best solution is to check for permission on service, and show notification if the permission is missing. Even better, create an DialogActivity to request for permission when users press on the notification.
Have a look at PermissionEverywhere library. It allows you to request permission from any context.
It creates a notification clicking on which it opens up an activity asking for permission.
Sample code from library's github page:-
#Override
protected Boolean doInBackground(Void... params) {
PermissionResponse response = PermissionEverywhere.getPermission(getApplicationContext(),
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQ_CODE,
"Notification title",
"This app needs a write permission",
R.mipmap.ic_launcher)
.call();
//waits..
boolean isGranted = response.isGranted();
if(isGranted){ //changed from isGrante to isGranted
// Do stuff
}
}
There is a very simple library that allows doing exactly this. You can check for permissions from anywhere (even from a service), based on whether the app is in foreground or background, it either shows normal dialog or generates a notification asking for permissions. The code is really easy to understand and it's really easy to use too.
Do give it a try: Android Permissions
You can use ResultReceiver to create a receiver of the users answer, then pass it as callback to the Activity, through notification's PendingIntent.
Reference
Suppose there are two apps on my device, GoodApp and EvilApp. I didn't write either of them. Both apps can use an ACTION_SEND Intent to start an exported activity in my app, passing data in. They both call startActivity, not startActivityForResult.
I want to trust the data I receive from GoodApp, but distrust and ignore the data I receive from EvilApp.
How can my Activity tell which app started it? Activity.getCallingActivity() returns null, because it wasn't started for-result. I can't send a message back to the apps asking "was it you who sent me this?" because they aren't written to respond to that, and their code is out of my control. Is there any way I can tell the difference?
Require apps use startActivityForResult in order for you to handle their request? (:
ActivityManager.getRunningTasks might give you a hint, but its documentation says not to use it for things like what you are asking.
i saw some applications with a little dialog asking for permit the app to listen for notification. That dialog got 2 button: cancel, and go (that opens the security settings to allow apps for listen for notification). That dialog is persisten so i guess it have a sort of method to detect if the app is allowed or not. Anyone can point me to that API? Thanks
I know this is an old question, but here what I use now in my application:
String notificationListenerString = Settings.Secure.getString(this.getContentResolver(),"enabled_notification_listeners");
//Check notifications access permission
if (notificationListenerString == null || !notificationListenerString.contains(getPackageName()))
{
//The notification access has not acquired yet!
}else{
//Your application has access to the notifications
}
You can move the user to Notification Access Permission settings by open the activity:
startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
This is tested very well from Jelly Bean 4.3 to Marshmallow 6.0 and I use it in my applications.
Hi I don't think that there is a method to call to know if you have permission to Listen Notifications, but you can try the following:
Try to acquire the reference of your NotificationListenerService instance.
Now if you got a null pointer when you expected it to be not null then you should prompt a Dialog asking user to enable the Security setting.
add onClickListener in "Ok" button and now just startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
All applications on Android can send notifications, there is not even a Permission for it. Any dialog you see in UIs is something implemented by each developer (to be extra considerate).
Bottom line, there is no API for accessing if an app can send notifications (all can).
Otherwise, there are Application Permissions for a variety of other things, which would also be worth learning about.