2013-10-21 23 views
3

我試圖編寫自己的Authenticator並將它用作具有兩個不同應用程序的庫:A和B. 我已按照此帖發表:http://udinic.wordpress.com/2013/04/24/write-your-own-android-authenticator/。 我安裝了應用程序A,然後安裝了應用程序B.當應用程序A調用AccountManager.addAccount()時,AccountAuthenticatorActivity打開。當應用程序B調用AccountManager.addAccount()時沒有任何事情發生。如果我卸載應用程序A並在應用程序B中再次嘗試,則AccountAuthenticatorActivity將打開。無法讓我的AccountAuthenticatorActivity在兩個不同的應用程序中打開

我的目標是要使用的兩個應用程序相同的AccountAuthenticatorActivity和AccountAuthenticator,但它似乎在一個應用程序在同一時間只工作。

這是addAccount我AbstractAccountAuthenticator:

@Override 
public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, String authTokenType, String[] requiredFeatures, Bundle options) throws NetworkErrorException { 

    final Intent intent = new Intent(mContext, AuthenticatorActivity.class); 
    intent.putExtra(AuthenticatorActivity.ARG_ACCOUNT_TYPE, accountType); 
    intent.putExtra(AuthenticatorActivity.ARG_AUTH_TYPE, authTokenType); 
    intent.putExtra(AuthenticatorActivity.ARG_IS_ADDING_NEW_ACCOUNT, true); 
    intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response); 

    final Bundle bundle = new Bundle(); 
    bundle.putParcelable(AccountManager.KEY_INTENT, intent); 
    return bundle; 
} 

這是我怎麼稱呼accountManager.addAccount()從我的兩個應用程序:

private void addNewAccount(String accountType, String authTokenType) { 
    final AccountManagerFuture<Bundle> future = mAccountManager.addAccount(accountType, authTokenType, null, null, this, new AccountManagerCallback<Bundle>() { 
     @Override 
     public void run(AccountManagerFuture<Bundle> future) { 
      try { 
       Bundle bnd = future.getResult(); 

      } catch (Exception e) { 
       e.printStackTrace(); 

      } 
     } 
    }, null); 
} 

回答

4

我有同樣的問題,關鍵所有這一切的混亂是AndroidManifest.xml中

教程就如何創建自定義的驗證器是一種舊的(或至少前的Android工作室),所以他們指出,您必須將權限,活動和服務從認證程序庫複製到所有將要使用該程序庫的應用程序中,因爲Android Studio會自動合併所有模塊的清單。

如果您使用的是Android工作室所有你需要做的是讓認證模塊中的權限,活動和服務,只需添加一個依賴於應用程序模塊。

下面是一個示例的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="18" /> 

<uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
<uses-permission android:name="android.permission.USE_CREDENTIALS" /> 
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" /> 
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

<application 
    android:allowBackup="true" 
    android:label="@string/auth_app_name" 
    android:theme="@style/Holo.Theme.Light.DarkActionBar"> 

    <activity 
     android:name="ro.muerte.modules.auth.MyAuthenticatorActivity" 
     android:exported="true" 
     android:label="@string/auth_action_login" 
     android:windowSoftInputMode="adjustResize|stateVisible" 
     android:clearTaskOnLaunch="true" /> 


    <service 
     android:name="ro.muerte.modules.auth.MyAuthenticateService" 
     android:exported="true"> 
     <intent-filter> 
      <action android:name="android.accounts.AccountAuthenticator" /> 
     </intent-filter> 
     <meta-data 
      android:name="android.accounts.AccountAuthenticator" 
      android:resource="@xml/authenticator" /> 
    </service> 

</application> 

+1

我錯過了 「出口=真」 在我的認證活動的價值。謝謝! – user1787773

+0

很高興我能幫上忙 –

相關問題