简体   繁体   English

在获取帐户android的身份验证令牌时显示对话框

[英]Show dialog when getting auth token for account android

I decided to show dialog with message "Authorization" when my app retrieving auth token for google account from account manager,then I'll want to update dialog setting message "Save contacts" and at this time perform long operation of saving contacts,but when I tying to make this like in code below my Auth dialog doesn't show(it takes 8 sec of black screen then dialog appear for one second and dismiss).I'm creating and updating dialog in the handler thread,which is bind to UI thread(create handler in onCreate method).It looks like UI thread blocked when I creating dialog.Thanks. 我决定从我的应用程序从帐户管理器检索google帐户的身份验证令牌时,显示带有消息“授权”的对话框,然后我要更新对话框设置消息“保存联系人”,这时需要进行长时间的保存联系人操作,但是当我想使它像我的“身份验证”对话框下面的代码中所示(它需要8秒钟的黑屏,然后对话框出现一秒钟然后关闭)。我正在处理程序线程中创建和更新对话框,该对话框绑定到UI线程(在onCreate方法中创建处理程序)。当我创建对话框时,UI线程似乎被阻塞了。 This is my code: 这是我的代码:

public void gotAccount(final GoogleAccountManager googleAccountManager,
        final Account account)
{

    SharedPreferences settings = getSharedPreferences(PREF, 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putString("accountName", account.name);
    editor.commit();
    Log.i("gotAccount","start new thread");

    new Thread(new Runnable()
    {

        public void run()
        {
            Log.i("gotAccount-Thread run()","start show loading message");
            createLoadingMessage();
            Log.i("gotAccount-Thread run()","complete show loading message");
            Log.i("gotAccount-Thread run()","start getAuthToken");
            googleAccountManager.manager
            .getAuthToken(account, AUTH_TOKEN_TYPE, true, new AccountManagerCallback<Bundle>()
            {

                @Override
                public void run(AccountManagerFuture<Bundle> future)
                {
                    try
                    {
                        Log.i("gotAccount-run()","get auth token complete");
                        Log.i("callback-run()","start get result");
                        Bundle bundle = future.getResult();

                        if (bundle.containsKey(AccountManager.KEY_INTENT))
                        {
                            Intent intent = bundle
                                    .getParcelable(AccountManager.KEY_INTENT);
                            int flags = intent.getFlags();
                            flags &= ~Intent.FLAG_ACTIVITY_NEW_TASK;
                            intent.setFlags(flags);
                            startActivityForResult(intent, REQUEST_AUTHENTICATE);

                        } else if (bundle.containsKey(AccountManager.KEY_AUTHTOKEN))
                        {
                            createSaveContactsMessage();

                            setAuthToken(bundle
                                    .getString(AccountManager.KEY_AUTHTOKEN));

                            longOperation();
                            completeSave(getText(R.string.saved) + ": "
                                    + currentCount);

                        }
                    } catch (Exception e)
                    {
                        handleException(e);
                    }
                }

            },null);
        }
    }).start();
}

There is easir way to deal with threads. 有简单的方法来处理线程。 As for example AsyncTask, where you define your progress bar in on preExecute(), do stuff that needs to be done doInBackground(), and remove dialog in onPostExecute(). 以AsyncTask为例,您可以在preExecute()中定义进度条,执行doInBackground()需要完成的工作,并在onPostExecute()中删除对话框。

More info about async task 有关异步任务的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM