gpt4 book ai didi

android - 如何显示从状态栏通知开始的权限 Activity ?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:53:11 27 4
gpt4 key购买 nike

我正在使用 AccountManager 获取 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);

如果 token 是第一次获取,AccountManager 会在状态栏中显示一个通知,它会启动一个请求访问帐户权限的 Activity 。

问题是,如何在不点击通知的情况下直接显示这个 Activity ?

最佳答案

在这里找到解决方案:

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 - 如何显示从状态栏通知开始的权限 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7737949/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com