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 6792967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:02:32+00:00 2026-05-26T18:02:32+00:00

This code is causing ANR force close any idea how to improve this code?

  • 0

This code is causing ANR force close any idea how to improve this code? i try with asynctask and i cant make it work in this code :

What i try to do here is updater activity will check for latest version and if got new version it will pop up alertdialog to ask user to update in the market

public class Updater extends Activity {

    private int newVerCode = 0;
    private String newVerName = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        if (getServerVerCode()) {
            int vercode = Config.getVerCode(this);
            if (newVerCode > vercode) {
                doNewVersionUpdate();

            } else {
                notNewVersionShow();
            }
        }

    }
//check version using json
    private boolean getServerVerCode() {
        try {
            String verjson = NetworkTool.getContent(Config.UPDATE_SERVER
                    + Config.UPDATE_VERJSON);
            JSONArray array = new JSONArray(verjson);
            if (array.length() > 0) {
                JSONObject obj = array.getJSONObject(0);
                try {
                    newVerCode = Integer.parseInt(obj.getString("verCode"));
                    newVerName = obj.getString("verName");
                } catch (Exception e) {
                    newVerCode = -1;
                    newVerName = "";
                    return false;
                }
            }
        } catch (Exception e) {

            return false;

        }
        return true;
    }
//Found No new version
    private void notNewVersionShow() {
        Updater.this.finish(); // End updater activity
    }

//Found New version
    private void doNewVersionUpdate() {
//Display alertdialog
    }
}
  • 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-05-26T18:02:33+00:00Added an answer on May 26, 2026 at 6:02 pm

    You can use an AsyncTask – yes. In doInBackground you can add the code from getServerVerCode() and in onPostExecute everything in the if (getServerVerCode()).
    doInBackground can return boolean so you know in onPostExecute what the result is.
    Something like this:

    private class GetServerVerCode extends AsyncTask<Void, Void, Boolean> { 
    
        @Override 
        protected Boolean doInBackground(Void... params) {
          try {
            String verjson = NetworkTool.getContent(Config.UPDATE_SERVER
                    + Config.UPDATE_VERJSON);
            JSONArray array = new JSONArray(verjson);
            if (array.length() > 0) {
                JSONObject obj = array.getJSONObject(0);
                try {
                    newVerCode = Integer.parseInt(obj.getString("verCode"));
                    newVerName = obj.getString("verName");
                } catch (Exception e) {
                    newVerCode = -1;
                    newVerName = "";
                    return false;
                }
            }
        } catch (Exception e) {
    
            return false;
    
        }
        return true;
        } 
    
        @Override
        protected void onPostExecute(Boolean result) {
            if (result) {
                int vercode = Config.getVerCode(this);
                if (newVerCode > vercode) {
                    doNewVersionUpdate();
    
                } else {
                    notNewVersionShow();
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code is causing a null pointer exception. I have no idea why: private
This code actually used to work until now. I have no idea what is
This morning we found an old chunk of code that was causing a library
This line in YUI's Reset CSS is causing trouble for me: address,caption,cite,code,dfn,em,strong,th,var { font-style:
I want to create maintainable code, but this inheritance situation is causing me problems.
Possible Duplicate: Why is this C code causing a segmentation fault? char* string =
I've stumbled accross this code in production and I think It may be causing
this simple code is causing the sp to not compile with all sorts of
I have this code: chars = #some list try: indx = chars.index(chars) except ValueError:
I'm using this code, to make a request to a given URL: private static

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.