Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8011343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:59:20+00:00 2026-06-04T18:59:20+00:00

I have an Android project in which I get a token from the server

  • 0

I have an Android project in which I get a token from the server for authentication and use it for further queries to the server. The token expires approximately in 10 minutes so I have to fetch a new one. My current pattern is like that:

1- Fetch the token when the user first starts the application

2- Block the user input with a “Please Wait” dialog so he does not try to send queries with a null token from other activites

3- Save the token and the timestamp, stop blocking user input

4- Check the timestamp regularly in onCreate() methods of the activities

5- Update if necessary.

My problem is this; if user opens an activity and the token is expired my application again blocks the user input and tries to fetch the token. However, in background, normal logic/flow of the application continues and it tries to send a query to the server with an old or a null token.

So I want to block this query task -which is an AsyncTask like the token fetching- and start it after the token fetching is finished. How can i use this pattern and is this a correct one?

Making one thread wait for another to finish?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T18:59:21+00:00Added an answer on June 4, 2026 at 6:59 pm

    I would consider using the Android Account manager with a custom authenticator for handling the tokens. The SampleSyncAdapter shows you how to proceed with that. Use the AccountManager‘s getAuthToken to fetch the current token. In the authenticator you can include logic to invalidate tokens at timeout which will cause the authenticator at the next request for a token to contact the server.

    But I guess it could be too much overhead for your project to implement this. Two approaches I see for your current setup:

    1. Maybe you can try to simply cancel the current query and fire a new one after you have retrieved a new token?
    2. Use Java synchronization primitives to block. E.g., create a “lock” object and use it in the async task and where you update your token:

      protected Result doInBackground(Params... params) {
          //
          // the request and other stuff that does not require the token.
      
          synchronized(lock) {
              //
              // communicate with the server.
          }
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing an android project in which I have to load from group
I have an Android project for which I use the Eclipse IDE and I
i have an android project which has a lot of classes I've built. i
I have Android pet-project DroidIn which utilizes HttpClient 4 (built into Android) to do
I have a PhoneGap 1.4.1 / jQueryMobile 1.0.1 / Android project which is showing
I have an Android project in eclipse IDE which was working. After some imports
I have an Android project that uses a library project (all of which I
I have an Android Project called Hello on my Ubuntu 10.04 i386 Server (headless).
I have an Android project (targeting Android 1.6 and up) which includes native code
Consider the following situation: I have two android projects named P1 and P2 which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.