2011-03-09 47 views
0

很多代碼...我從Android的官方示例代碼中獲得了這個,SampleSyncAdapter無法讓我的帳戶在Android帳戶中顯示......任何人都可以看看嗎?

反正這裏是代碼。我沒有包含同步類,我只想讓帳號出現並能夠立即創建。

帳戶身份驗證類:

package com.tagapp.android; 

import android.accounts.AbstractAccountAuthenticator; 
import android.accounts.Account; 
import android.accounts.AccountAuthenticatorResponse; 
import android.accounts.AccountManager; 
import android.accounts.NetworkErrorException; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 

public class AccountAuthenticator extends AbstractAccountAuthenticator { 

    private Context mContext; 

    public AccountAuthenticator(Context context) { 
     super(context); 
     mContext = context; 
    } 

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

     Intent intent = new Intent(mContext, AuthenticatorActivity.class); 
     intent.putExtra(AuthenticatorActivity.PARAM_AUTHTOKEN_TYPE, authTokenType); 
     intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response); 
     Bundle bundle = new Bundle(); 
     bundle.putParcelable(AccountManager.KEY_INTENT, intent); 
     return bundle; 
    } 

    @Override 
    public Bundle confirmCredentials(AccountAuthenticatorResponse response, 
      Account account, Bundle options) throws NetworkErrorException { 

     if(options != null && options.containsKey(AccountManager.KEY_PASSWORD)) { 
      String password = options.getString(AccountManager.KEY_PASSWORD); 
      boolean verified = onlineConfirmPassword(account.name, password); 
      Bundle result = new Bundle(); 
      result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, verified); 
      return result; 
     } 

     Intent intent = new Intent(mContext, AuthenticatorActivity.class); 
     intent.putExtra(AuthenticatorActivity.PARAM_USERNAME, account.name); 
     intent.putExtra(AuthenticatorActivity.PARAM_CONFIRM_CREDENTIALS, true); 
     intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response); 
     Bundle bundle = new Bundle(); 
     bundle.putParcelable(AccountManager.KEY_INTENT, intent); 
     return bundle; 
    } 

    private boolean onlineConfirmPassword(String username, String password) { 
     return RestClient.authenticate(username, password, null, null); 
    } 

    @Override 
    public Bundle editProperties(AccountAuthenticatorResponse response, 
      String accountType) { 
     throw new UnsupportedOperationException(); 
    } 

    @Override 
    public Bundle getAuthToken(AccountAuthenticatorResponse response, 
      Account account, String authTokenType, Bundle options) { 

     if(!authTokenType.equals("com.tagapp.android")) { 
      Bundle result = new Bundle(); 
      result.putString(AccountManager.KEY_ERROR_MESSAGE, "invalid authtokenType"); 
      return result; 
     } 

     AccountManager am = AccountManager.get(mContext); 
     String password = am.getPassword(account); 
     if(password != null) { 
      boolean verified = onlineConfirmPassword(account.name, password); 
      if(verified) { 
       Bundle result = new Bundle(); 
       result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); 
       result.putString(AccountManager.KEY_ACCOUNT_TYPE, "com.tagapp.android"); 
       result.putString(AccountManager.KEY_AUTHTOKEN, password); 
       return result; 
      } 
     } 

     Intent intent = new Intent(mContext, AuthenticatorActivity.class); 
     intent.putExtra(AuthenticatorActivity.PARAM_USERNAME, account.name); 
     intent.putExtra(AuthenticatorActivity.PARAM_AUTHTOKEN_TYPE, authTokenType); 
     intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response); 
     Bundle bundle = new Bundle(); 
     bundle.putParcelable(AccountManager.KEY_INTENT, intent); 
     return bundle; 
    } 

    @Override 
    public String getAuthTokenLabel(String authTokenType) { 
     if("com.tagapp.android".equals(authTokenType)) { 
      return "Tag"; 
     } 
     return null; 
    } 

    @Override 
    public Bundle hasFeatures(AccountAuthenticatorResponse response, 
      Account account, String[] features) throws NetworkErrorException { 

     Bundle result = new Bundle(); 
     result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false); 
     return result; 
    } 

    @Override 
    public Bundle updateCredentials(AccountAuthenticatorResponse response, 
      Account account, String authTokenType, Bundle options) { 

     Intent intent = new Intent(mContext, AuthenticatorActivity.class); 
     intent.putExtra(AuthenticatorActivity.PARAM_USERNAME, account.name); 
     intent.putExtra(AuthenticatorActivity.PARAM_AUTHTOKEN_TYPE, authTokenType); 
     intent.putExtra(AuthenticatorActivity.PARAM_CONFIRM_CREDENTIALS, false); 
     Bundle bundle = new Bundle(); 
     bundle.putParcelable(AccountManager.KEY_INTENT, intent); 
     return bundle; 
    } 


} 

AccountAuthenticationActivity類:

package com.tagapp.android; 

import android.accounts.Account; 
import android.accounts.AccountAuthenticatorActivity; 
import android.accounts.AccountManager; 
import android.app.Dialog; 
import android.app.ProgressDialog; 
import android.content.ContentResolver; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.os.Bundle; 
import android.os.Handler; 
import android.provider.ContactsContract; 
import android.text.TextUtils; 
import android.view.View; 
import android.view.Window; 
import android.widget.EditText; 
import android.widget.TextView; 

公共類AuthenticatorActivity擴展AccountAuthenticatorActivity {

public static final String PARAM_AUTHTOKEN_TYPE = "authtokenType"; 
public static final String PARAM_USERNAME = "username"; 
public static final String PARAM_CONFIRM_CREDENTIALS = "confirmCredentials"; 
public static final String PARAM_PASSWORD = "password"; 

private AccountManager mAccountManager; 
private Thread mAuthThread; 
private String mAuthtoken; 
private String mAuthtokenType; 
private Boolean mConfirmCredentials = false; 

private final Handler mHandler = new Handler(); 

private TextView mMessageView; 
private String mPassword; 
private String mUsername; 
private EditText mPasswordEdit; 
private EditText mUsernameEdit; 

private boolean mRequestNewAccount = false; 

@Override 
public void onCreate(Bundle neato) { 
    super.onCreate(neato); 
    mAccountManager = AccountManager.get(this); 
    Intent intent = getIntent(); 
    mUsername = intent.getStringExtra(PARAM_USERNAME); 
    mAuthtokenType = intent.getStringExtra(PARAM_AUTHTOKEN_TYPE); 
    mConfirmCredentials = intent.getBooleanExtra(PARAM_CONFIRM_CREDENTIALS, false); 

    initLayout(); 
} 

private void initLayout() { 
    setContentView(R.layout.account_sync_login); 
    requestWindowFeature(Window.FEATURE_LEFT_ICON); 
    mMessageView = (TextView)findViewById(R.id.account_login_message); 
    mUsernameEdit = (EditText)findViewById(R.id.account_login_username_edittext); 
    mPasswordEdit = (EditText)findViewById(R.id.account_login_password_edittext); 
    mUsernameEdit.setText(mUsername); 
    if(getMessage() != null) { 
     mMessageView.setText(getMessage()); 
    } 
} 

private CharSequence getMessage() { 
    if(TextUtils.isEmpty(mUsername)) { 
     CharSequence msg = "Enter your username and password."; 
     return msg; 
    } 
    if(TextUtils.isEmpty(mPassword)) { 
     return "Enter your password."; 
    } 
    return null; 
} 

@Override 
protected Dialog onCreateDialog(int id) { 
    ProgressDialog dialog = new ProgressDialog(this); 
    dialog.setMessage("Authenticating..."); 
    dialog.setIndeterminate(true); 
    dialog.setCancelable(true); 
    dialog.setOnCancelListener(new DialogInterface.OnCancelListener() { 

     @Override 
     public void onCancel(DialogInterface dialog) { 
      if(mAuthThread != null) { 
       mAuthThread.interrupt(); 
       finish(); 
      } 
     } 

    }); 

    return dialog; 
} 

public void handleLogin(View v) { 
    if(mRequestNewAccount) { 
     mUsername = mUsernameEdit.getText().toString(); 
    } 
    mPassword = mPasswordEdit.getText().toString(); 
    if(TextUtils.isEmpty(mUsername) || TextUtils.isEmpty(mPassword)) { 
     mMessageView.setText(getMessage()); 
    } 
    else { 
     showProgress(); 
     mAuthThread = RestClient.attemptAuth(mUsername, mPassword, mHandler, this); 
    } 
} 

private void finishConfirmCredentials(boolean result) { 
    Account account = new Account(mUsername, "com.tagapp.android"); 
    mAccountManager.setPassword(account, mPassword); 
    Intent intent = new Intent(); 
    intent.putExtra(AccountManager.KEY_BOOLEAN_RESULT, result); 
    setAccountAuthenticatorResult(intent.getExtras()); 
    setResult(RESULT_OK, intent); 
    finish(); 
} 

private void finishLogin() { 
    Account account = new Account(mUsername, "com.taggapp.android"); 
    if(mRequestNewAccount) { 
     mAccountManager.addAccountExplicitly(account, mPassword, null); 
     ContentResolver.setSyncAutomatically(account, ContactsContract.AUTHORITY, true); 
    } 
    else { 
     mAccountManager.setPassword(account, mPassword); 
    } 
    Intent intent = new Intent(); 
    mAuthtoken = mPassword; 
    intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, mUsername); 
    intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, "com.tagapp.android"); 
    if(mAuthtokenType != null && mAuthtokenType.equals("com.tagapp.android")) { 
     intent.putExtra(AccountManager.KEY_AUTHTOKEN, mAuthtoken); 
    } 
    setAccountAuthenticatorResult(intent.getExtras()); 
    setResult(RESULT_OK, intent); 
    finish(); 
} 

private void showProgress() { 
    showDialog(0); 
} 

private void hideProgress() { 
    dismissDialog(0); 
} 

public void onAuthenticationResult(boolean result) { 
    hideProgress(); 
    if(result) { 
     if(!mConfirmCredentials) { 
      finishLogin(); 
     } 
     else { 
      finishConfirmCredentials(true); 
     } 
    } 
    else { 
     if(mRequestNewAccount) { 
      mMessageView.setText("You got it all wrong."); 
     } 
     else { 
      mMessageView.setText("Wrong password."); 
     } 
    } 
} 

}

AuthenticatorService類:

package com.tagapp.android; 

import android.app.Service; 
import android.content.Intent; 
import android.os.IBinder; 

public class AccountAuthenticatorService extends Service { 

    private AccountAuthenticator mAuthenticator; 

    @Override 
    public void onCreate() { 
     mAuthenticator = new AccountAuthenticator(this); 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     return mAuthenticator.getIBinder(); 
    } 
} 

清單包括以下內容:

<service android:name=".authenticator.AccountAuthenticatorService" 
      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> 

和...

<activity android:name=".AuthenticatorActivity" 
     android:label="Sync Tag Account!" 
     android:theme="@android:style/Theme.Dialog" 
     android:excludeFromRecents="true"> 

在服務中引用的XML文件是在這裏:

<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android" 
    android:accountType="com.tagapp.android" 
    android:icon="@drawable/taglogo" 
    android:smallIcon="@drawable/launchicon" 
    android:label="Tag"/> 

我知道這是很多代碼......但我已經變得非常絕望,試圖爲此找到結果。據我所知,這應該工作得很好,但是當我去添加一個帳戶它超時/崩潰,或者只是不顯示我的應用程序的帳戶作爲一個選項添加。我已經在模擬器和我的實際Android手機上嘗試了這兩種方法,但都沒有成功。 Logcat出現故障時不會產生錯誤。如果有人有這方面的經驗,並可以提供一些示例代碼/洞察力,爲什麼我的代碼不工作,我真的很感激它......非常感謝!

回答

0

我沒有看到你的列接口文件。它定義了您的自定義MIME_PROFILE以及您的聯繫人數據存儲在數據表中的哪些列。

+0

需要將這些帳戶出現在帳戶列表中嗎?我知道我發佈的代碼在進行同步時並不完整,但我希望能檢查我的進度並確保該帳戶出現在可用帳戶列表中。我是否需要列類來使其顯示?列真的很簡單吧?就像4弦IIRC ...對嗎? – JMRboosties 2011-03-09 08:44:00

+0

@JMRboosties您將此答案標記爲正確 - 您可以通過回答您發佈的這些問題來詳細說明嗎? – ataulm 2015-11-08 15:52:27

相關問題