码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-11-28 20:11:05      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:android开发   framework   


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

bubuko.com,布布扣


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

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


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中找到解决办法,代码就在那里,看你如何去发现。


以上。




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

标签:android开发   framework   

原文地址:http://blog.csdn.net/eclipsexys/article/details/41576989

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!