How to display an action for permission that starts with a status bar notification?

I am using AccountManager to get OAuth 2.0 Token.

mAccountManager.getAuthToken(mAccount, AUTH_TOKEN_TYPE, true, new AccountManagerCallback<Bundle>() { @Override public void run(AccountManagerFuture<Bundle> future) { Bundle bundle = future.getResult(); if (bundle.containsKey(AccountManager.KEY_AUTHTOKEN)) { String authToken = future.getResult().getString(AccountManager.KEY_AUTHTOKEN); ... continue } } } }, null); 

If the token is received for the first time, the AccountManager displays a notification in the status bar, in which the action begins, requesting permission to access the account.

The question is, how can I immediately display this activity without clicking "Notification"?

If someone does not have specific information about this, but useful links, write.

+4
source share
2 answers

Found a solution here:

http://www.finalconcept.com.au/article/view/android-account-manager-using-other-accounts

Here's a copy of the code in case the page goes away:

 private final Handler handler = new Handler(); /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { final AccountManager accMgr; final Account[] accounts; final Account account; final AccountManagerFuture<Bundle> amf; final MainActivity cbt = this; String authTokenType; super.onCreate(savedInstanceState); this.setContentView(R.layout.main); accMgr = AccountManager.get(this); authTokenType = "com.google"; accounts = accMgr.getAccountsByType(authTokenType); account = accounts[2]; amf = accMgr.getAuthToken(account, authTokenType, true, new AccountManagerCallback<Bundle>() { @Override public void run(AccountManagerFuture<Bundle> arg0) { try { Bundle result; Intent i; String token; result = arg0.getResult(); if (result.containsKey(AccountManager.KEY_INTENT)) { i = (Intent)result.get(AccountManager.KEY_INTENT); if (i.toString().contains("GrantCredentialsPermissionActivity")) { // Will have to wait for the user to accept // the request therefore this will have to // run in a foreground application cbt.startActivity(i); } else { cbt.startActivity(i); } } else { token = (String)result.get(AccountManager.KEY_AUTHTOKEN); /* * work with token */ // Remember to invalidate the token if the web service rejects it // if(response.isTokenInvalid()){ // accMgr.invalidateAuthToken(authTokenType, token); // } } } catch (OperationCanceledException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (AuthenticatorException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }, handler); } 

This page was also helpful:

Google Account Authenticator Requesting Runtime Permission

+5
source

The notification is used in most cases from the service. If you want to immediately show the action, you cannot do it from the service. But there is no problem to immediately show this activity when you are already showing one of your applications right now for the user. And in this case no notifications are required, so just pass "get" as the notification argument to getAuthToken () or the call method without this parameter in the signature. If you want to know in which case this activity is accurately shown, below is one of them.

It is just this screen that appears when AccountManager.getAuthToken () is called to access an account created in another application (with a different signature). To show that the view automatically simply passes the action to AccountManager.getAuthToken () or starts it from the received KEY_INTENT, as indicated in user979247's answer.

0
source

All Articles