2016-03-26 14 views
0

今日、私のアプリケーションにはクラシックなログイン画面(ユーザー名とパスワード)があり、このクラスは共通のアクティビティを拡張しています。私の最初の活動はログインです(すでにログインしているユーザーがログインしている場合は自宅にリダイレクトされます)。Android - AccountManagerとLogin classic

マイアンドロイドマニフェスト:

<!-- Start Activity --> 
    <activity 
     android:name=".activity.LoginActivity" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme.NoActionBar" 
     android:windowSoftInputMode="adjustPan"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

そして今、私は、Androidのアカウントマネージャを展開しようとしているが、マニュアルを読み、実装します。この機能は大丈夫です。 Androidアカウントの設定を入力すると、アプリにログインしてアカウントを作成することができます。しかし、私の質問は両方の方法を維持する方法です。

facebookとして、ユーザーがアプリケーションを開くと、ログインして自動的にAccountManager上にアカウントが作成され、設定でアカウントに取得され、アプリケーションにも記録されます。バレーボールを使用してサーバー上でユーザーの検証とパスワードを要求したら、どのような手順を実行する必要がありますか?データをAccountManagerに返すか、アカウントを追加しますか?

ログインクラシック、サーバーの検証は「addAccountExplicitly」を使用してアカウントを追加した後:あなたがいない両方の方法を保つために

Bundle data = new Bundle(); 
     data.putString(AccountManager.KEY_ACCOUNT_NAME, email); 
     data.putString(AccountManager.KEY_ACCOUNT_TYPE, accountType); 
     data.putString(AccountManager.KEY_AUTHTOKEN, authToken); 
     data.putString(PARAM_USER_PASS, password); 
     final Intent intent = new Intent(); 
     intent.putExtras(data); 

     final Account account = new Account(email, accountType); 

     if (getIntent().getBooleanExtra(ARG_IS_ADDING_NEW_ACCOUNT, false)) { 
      Log.d(TAG, " finishLogin > addAccountExplicitly"); 
      // Creating the account on the device and setting the auth token we got 
      // (Not setting the auth token will cause another call to the server to authenticate the user) 
      mAccountManager.addAccountExplicitly(account, password, null); 
      mAccountManager.setAuthToken(account, mAuthTokenType, authToken); 
     } else { 
      Log.d(TAG, " finishLogin > setPassword"); 
      mAccountManager.setPassword(account, password); 
     } 

     setAccountAuthenticatorResult(intent.getExtras()); 
     setResult(RESULT_OK, intent); 
     finish(); 

答えて

2

:アカウント設定の

private void finishLogin(final String email, final String authToken) { 
     Account account = new Account(email, AccountGeneral.ACCOUNT_TYPE); 
     boolean success = mAccountManager.addAccountExplicitly(account, "", null); 
     if (success) { 
      Log.d(TAG, "Account created"); 
      mAccountManager.setAuthToken(account, mAuthTokenType, authToken); 
     } else { 
      Log.d(TAG, "Account creation failed. Look at previous logs to investigate"); 
     } 
} 

とログインを2つの活動を持ち、2つのシナリオを処理する必要があります。ログインプロセスのすべてを統合し、アカウントデータを設定し、明示的にアカウントをアカウントマネージャーパッケージに追加するだけです。 (到達可能な設定 - >アカウントの追加)。

あなたのアプリケーションでは、AccountManagerクラスとは異なるオプションがあります。

既にアカウントがあるかどうかは、getAccountsByType()で確認できます。

AddAccount()に電話すると、アプリにアカウントを追加できます。 [設定]から表示される同じアクティビティが自動的に表示されます。また、選択したアカウント名とauthTokenを返すコールバックもあります。

またnewChooseAccountIntent()に電話することができます。アカウントがない場合、この方法ではログインアクティビティが自動的に表示され、アカウントがすでに追加されている場合はリストとして表示され、ユーザーはアカウントを選択できるため、非常に良いオプションです。