程序员人生 网站导航

调用系统中Account类型的登录界面

栏目:综合技术时间:2014-12-09 08:02:50


        我们的手机中的Seting里面可以添加很多类型的帐户,有时候,我们需要在程序中启动这些帐户的登录界面,但是,就算你知道了登录界面的包名、类名,通过Intent也没办法启动,这是由于Android的权限控制。



        不过,我们在Framework中也找到了解决的办法,比如我们要启动Google Account的登录界面,我们只需要知道该帐户的类型就能够了,像Google就是com.google:

下面我们来看看如何启动这样1个界面:


private void setupAccount(String type) { Bundle addAccountOptions = new Bundle(); mPendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(), 0); addAccountOptions.putParcelable(KEY_CALLER_IDENTITY, mPendingIntent); addAccountOptions.putBoolean(EXTRA_HAS_MULTIPLE_USERS, hasMultipleUsers(this)); AccountManager.get(this).addAccount( type, null, /* authTokenType */ null, /* requiredFeatures */ addAccountOptions, null, mCallback, null /* handler */); } public boolean hasMultipleUsers(Context context) { return ((UserManager) context.getSystemService(Context.USER_SERVICE)) .getUsers().size() > 1; } /** * Callback setting google account. */ private AccountManagerCallback<Bundle> mCallback = new AccountManagerCallback<Bundle>() { @Override public void run(AccountManagerFuture<Bundle> future) { try { Bundle bundle = future.getResult(); Intent intent = (Intent) bundle.get(AccountManager.KEY_INTENT); if (intent != null) { Bundle addAccountOptions = new Bundle(); addAccountOptions.putParcelable(KEY_CALLER_IDENTITY, mPendingIntent); addAccountOptions.putBoolean(EXTRA_HAS_MULTIPLE_USERS, hasMultipleUsers(KSWarning.this)); intent.putExtras(addAccountOptions); startActivityForResult(intent, 0); } } catch (OperationCanceledException e) { } catch (IOException e) { } catch (AuthenticatorException e) { } } };


我们通过调用setupAccount()将帐户类型传递进去就能够了。

这些代码都是Framework中的代码,我们略微修改了下就拿来用了。所以说,Framework是个宝藏,很多功能都可以在Framework中找到解决办法,代码就在那里,看你如何去发现。


以上。




------分隔线----------------------------
------分隔线----------------------------

最新技术推荐