2016-12-24 148 views
0

我在服务中使用帐户管理器帐户(它扩展了FirebaseInstanceIdService)。如果theres没有有效的帐户,然后我使用accountManager.addAccount添加帐户。Android帐户管理器从服务添加帐户

这需要作为参数活动(用于启动帐户登录活动)。但是,当我从服务中调用addAccount时,我没有当前的活动放置在那里。如何从服务中调用addAccount并让它在需要的地方显示帐户登录?

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService { 

    private static final String TAG = "MyFirebaseIIDService"; 

    @Override 
    public void onTokenRefresh() { 
     AccountManager accountManager = (AccountManager) getApplicationContext().getSystemService(ACCOUNT_SERVICE); 
     Account account[] = accountManager.getAccountsByType(ACCOUNT_TYPE); 
     if(account.length==0) { 
      Activity activity=???????//What can I set here 
      accountManager.addAccount(ACCOUNT_TYPE, AccountGeneral.AUTHTOKEN_TYPE_FULL_ACCESS, null, 
      null, activity, new AccountManagerCallback<Bundle>() { 
         public void run(AccountManagerFuture<Bundle> arg0) { 
         } 
        }, null); 
      return null; 
     } 
     //do stuff with ContentResolver using account 
    } 
} 
+0

编辑显示一些代码来说明问题 – Nathan

回答

0

addAccount()方法要求Activity实例调用默认的Authenticator活动。

它们是AccountManager类中另一种名为addAccountExplicitly()的方法。 下面是文档:

/** 
    * Adds an account directly to the AccountManager. Normally used by sign-up 
    * wizards associated with authenticators, not directly by applications. 
    * <p>Calling this method does not update the last authenticated timestamp, 
    * referred by {@link #KEY_LAST_AUTHENTICATED_TIME}. To update it, call 
    * {@link #notifyAccountAuthenticated(Account)} after getting success. 
    * However, if this method is called when it is triggered by addAccount() or 
    * addAccountAsUser() or similar functions, then there is no need to update 
    * timestamp manually as it is updated automatically by framework on 
    * successful completion of the mentioned functions. 
    * <p>It is safe to call this method from the main thread. 
    * <p>This method requires the caller to have a signature match with the 
    * authenticator that owns the specified account. 
    * 
    * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before, 
    * AUTHENTICATE_ACCOUNTS permission is needed for those platforms. See docs 
    * for this function in API level 22. 
    * 
    * @param account The {@link Account} to add 
    * @param password The password to associate with the account, null for none 
    * @param userdata String values to use for the account's userdata, null for 
    *   none 
    * @return True if the account was successfully added, false if the account 
    *   already exists, the account is null, or another error occurs. 
    */ 
    public boolean addAccountExplicitly(Account account, String password, Bundle userdata); 

用法:

创建帐户例如:

final Account account = new Account(accountName, intent.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE)); 

初始化帐户元数据:

String authtoken = //Generated Auth Token ; 
String authtokenType = //Auth Token type; 
String accountPassword= //Auth password if available; 

呼叫addAccountExplicitly()方法:

mAccountManager.addAccountExplicitly(account, accountPassword, //User data bundle); 
mAccountManager.setAuthToken(account, authtokenType, authtoken); 

这将是很好的去。祝你好运!

+0

感谢您的。我应该说,如果没有设置帐户并且需要添加新帐户,我希望打开用于输入凭据的验证活动。当需要在另一个活动中发生帐户时,addAccount是理想的选择,但当服务发生对帐户的需求时,我无法弄清楚如何获取活动实例。 – Nathan

+0

为此,如果帐户数组大小为零,我建议您使用服务意图自行启动活动。不需要依赖addAccount()方法,因为最终将在Authenticator活动中调用addAccount()方法。 –