开发者

How to display an activity for permission which starts from Status Bar Notification?

I'm using AccountManager for getting OAuth 2.0 token.

mAccountManager.getAuthToken(mAccount, AUTH_TOKEN_TYPE, true, new AccountManagerCallback<Bundle>() {
        @Override
        public void run(AccountManagerFuture<Bundle> future) {
                Bundle bundle = future.getRe开发者_运维问答sult();
                if (bundle.containsKey(AccountManager.KEY_AUTHTOKEN)) {
                    String authToken = future.getResult().getString(AccountManager.KEY_AUTHTOKEN);
                    ... continue
                }
            }
        }
    }, null);

If token is getting first time, AccountManager shows a notification in status bar, which starts an activity that requesting permission to access account.

The question is, how can I display this activity straight away without clicking on Notification?


Found the solution here:

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

Here's a copy of the code in case that 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 useful:

Google Account Authenticator asks for permission at run-time


Notification is used from service in most times. If you want to show an activity immediately you can't do that from service. But there is no problem to show that activity immediately, when you already showing one of your app's activities right now to user. And no notifications is needed in such case, so just pass "false" as notification argument to getAuthToken() or call method without that param in signature. If you want to know, in what case that activity is exactly shown, below is one of it.

Simply, this screen appears when AccountManager.getAuthToken() is called to access an account, created in another app (with another signature). To show that view automatically just pass an activity into AccountManager.getAuthToken() or start it from received KEY_INTENT, as suggested in user979247's reply.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜