简体   繁体   中英

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. 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().

More info about async task

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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