其他分享
首页 > 其他分享> > android – 如何显示从状态栏通知开始的权限活动?

android – 如何显示从状态栏通知开始的权限活动?

作者:互联网

我正在使用AccountManager获取OAuth 2.0令牌.

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);

如果令牌第一次到达,AccountManager会在状态栏中显示通知,该通知会启动请求访问帐户权限的活动.

问题是,如何在不点击通知的情况下直接显示此活动?

如果有人没有关于此的具体信息,但有用的链接,请发布.

解决方法:

在这里找到解决方案:

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

这是代码的副本,以防页面消失:

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);  

}  

这个页面也很有用:

Google Account Authenticator asks for permission at run-time

标签:android,accountmanager
来源: https://codeday.me/bug/20190621/1252855.html