Аутентификатор

Ответить
Looos
Сообщения: 1
Зарегистрирован: 07 сен 2014, 18:21

Аутентификатор

Сообщение Looos » 07 сен 2014, 18:54

Необходимо создать собственный аккаунт на устройстве.
Не понятно, почему не появляется пункт для добавления аккаунта в Settings -> Accounts -> Add Account
При запуске приложения появляется LoginActivity, после нажатия кнопки приложение падает.
Приложение крашиться на am.addAccountExplicitly(account, password, null); в обработчике кнопки

Выхлоп:

Код: Выделить всё

09-07 19:15:23.784: E/AndroidRuntime(3502): FATAL EXCEPTION: main
09-07 19:15:23.784: E/AndroidRuntime(3502): java.lang.SecurityException: caller uid 10044 is different than the authenticator's uid
09-07 19:15:23.784: E/AndroidRuntime(3502): 	at android.os.Parcel.readException(Parcel.java:1327)
09-07 19:15:23.784: E/AndroidRuntime(3502): 	at android.os.Parcel.readException(Parcel.java:1281)
09-07 19:15:23.784: E/AndroidRuntime(3502): 	at android.accounts.IAccountManager$Stub$Proxy.addAccount(IAccountManager.java:547)
09-07 19:15:23.784: E/AndroidRuntime(3502): 	at android.accounts.AccountManager.addAccountExplicitly(AccountManager.java:516)
09-07 19:15:23.784: E/AndroidRuntime(3502): 	at com.dtgame.coffeelike.LoginActivity.login(LoginActivity.java:62)
09-07 19:15:23.784: E/AndroidRuntime(3502): 	at com.dtgame.coffeelike.LoginActivity.access$0(LoginActivity.java:52)
09-07 19:15:23.784: E/AndroidRuntime(3502): 	at com.dtgame.coffeelike.LoginActivity$1.onClick(LoginActivity.java:39)
09-07 19:15:23.784: E/AndroidRuntime(3502): 	at android.view.View.performClick(View.java:3534)
09-07 19:15:23.784: E/AndroidRuntime(3502): 	at android.view.View$PerformClick.run(View.java:14263)
09-07 19:15:23.784: E/AndroidRuntime(3502): 	at android.os.Handler.handleCallback(Handler.java:605)
09-07 19:15:23.784: E/AndroidRuntime(3502): 	at android.os.Handler.dispatchMessage(Handler.java:92)
09-07 19:15:23.784: E/AndroidRuntime(3502): 	at android.os.Looper.loop(Looper.java:137)
09-07 19:15:23.784: E/AndroidRuntime(3502): 	at android.app.ActivityThread.main(ActivityThread.java:4441)
09-07 19:15:23.784: E/AndroidRuntime(3502): 	at java.lang.reflect.Method.invokeNative(Native Method)
09-07 19:15:23.784: E/AndroidRuntime(3502): 	at java.lang.reflect.Method.invoke(Method.java:511)
09-07 19:15:23.784: E/AndroidRuntime(3502): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-07 19:15:23.784: E/AndroidRuntime(3502): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-07 19:15:23.784: E/AndroidRuntime(3502): 	at dalvik.system.NativeStart.main(Native Method)
В манифесте прописал разрешения, uid и сервис

Код: Выделить всё

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.dtgame.product"
    android:versionName="1.0" android:sharedUserId="com.dtgame.product">
......
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS"/>
    <uses-permission android:name="android.permission.GET_ACCOUNTS"/>
    <uses-permission android:name="android.permission.MANAGE_ACCOUNTS"/>
    <uses-permission android:name="android.permission.USE_CREDENTIALS"/>
..... 
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.Base.AppCompat.Light.DarkActionBar">     
        <activity 
            android:name="com.dtgame.product.login.LoginActivity"
            android:screenOrientation="portrait">
        </activity>
.......
        <service android:name="com.dtgame.product.login.AuthenticatorService"
            	 android:exported="false">
            <intent-filter >
                <action android:name="android.accounts.AccountAuthenticator"/>
            </intent-filter>
            <meta-data android:name="android.accounts.AccountAuthenticator"
                android:resource="@xml/authenticator"/>
        </service>
    </application>

</manifest>
Файл /res/xml/authenticator.xml

Код: Выделить всё

<?xml version="1.0" encoding="utf-8"?>
<account-authenticator xmlns:android="http://schemas/android/com/apk/res/android"
    android:accountType="com.dtgame.product"
    android:icon="@drawable/ic_launcher"
    android:smallIcon="@drawable/ic_launcher"
    android:label="@string/product"/>
Создал Service

Код: Выделить всё

public class AuthenticatorService extends Service{
	private static final Object lock = new Object();
	private AccountAuthenticator auth;
	
	@Override
	public void onCreate() {
		super.onCreate();
		synchronized(lock){
			if(auth == null){
				auth = new AccountAuthenticator(this);
			}
		}
	}
	
	@Override
	public IBinder onBind(Intent intent) {
		return auth.getIBinder();
	}
}
Создал AccountAuthenticator переопределив метод addAccount

Код: Выделить всё

public class AccountAuthenticator extends AbstractAccountAuthenticator {

public final static String ACCOUNT_TYPE = "com.dtgame.product";

// .....
@Override
    public Bundle addAccount(AccountAuthenticatorResponse response,
            String accountType, String authTokenType,
            String[] requiredFeatures, Bundle options)
            throws NetworkErrorException {
		
		final Intent intent = new Intent(mContext, LoginActivity.class);
		intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
		final Bundle bundle = new Bundle();
		bundle.putParcelable(AccountManager.KEY_INTENT, intent);
		return bundle;
    }
//.....
}
Создал Activity для логина

Код: Выделить всё

public class LoginActivity extends AccountAuthenticatorActivity {
	
	private static final String TAG = "AAA";	
	
        private String username;
	private String password;
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.auth);
		((Button)findViewById(R.id.login)).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				login();
			}
		});
	}
	
	private Boolean isValidateDate(int id, String type){
		if(((EditText) findViewById(id)).getText() == null || ((EditText) findViewById(id)).getText().toString().trim().length() < 1){
			Toast.makeText(this, "Please enter a " + type, Toast.LENGTH_SHORT).show();
			return false;
			}
		return true;
	}
		
	private void login(){
		if(!isValidateDate(R.id.username, "Username") || !isValidateDate(R.id.password, "Password")){
			return;
		}
		
		username = ((EditText) findViewById(R.id.username)).getText().toString();
		password = ((EditText) findViewById(R.id.password)).getText().toString();
	
		final Account account = new Account(username, AccountAuthenticator.ACCOUNT_TYPE);
		AccountManager am = AccountManager.get(this);
		am.addAccountExplicitly(account, password, null);
		ContentResolver.setSyncAutomatically(account, ContactsContract.AUTHORITY, true);
		
	//	AccountManager.get(this).setPassword(account, PARAM_PASSWORD);
		
		Intent intent = new Intent();
		intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, username);
		intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, AccountAuthenticator.ACCOUNT_TYPE);
		setAccountAuthenticatorResult(intent.getExtras());
		setResult(RESULT_OK, intent);
		finish();
	}

}

Ответить