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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:46:43+00:00 2026-06-14T05:46:43+00:00

A game I wrote some time ago has a problem with ANRs, and debugging

  • 0

A game I wrote some time ago has a problem with ANRs, and debugging suggests they’re down to HTTP requests taking a long time (and thus causing the ANR).

I’d thought that by assigning the HTTP code into a Runnable called from within a Handler, I’d could avoid the ANR – but it seems this isn’t the case?

The stack dumps suggest the runnable/handler code is still running within the ‘Main’ thread and thus still causes ANRs??

The task it’s doing is asynchronous (uploading highscores and achievements) and so can be started and left to it’s own devices entirely – what is the best way to implement this so that ANRs aren’t going to become a problem?

One topic suggested that the Handler should be created in the Application class and not within the Game’s Activity – but I can’t find any detail on the differences between those cases??

All ideas greatly apprec.

p.s. extending this to ask – I assume an ANR relating to HTTP comes down to the phone being out-of-service/network/WiFi, because I’ve set a SHORT timeout for these requests (they’re non-essential and can be retried later!?)

  • 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-14T05:46:44+00:00Added an answer on June 14, 2026 at 5:46 am

    A Handler will execute code / handle messages per default (any constructor without Looper e.g. new Handler()) in the current thread. That is in almost every case the main thread. If you want it to execute in a different thread you have to tell it which Looper thread it should use.

    Android has a utility class called HandlerThread that creates a Thread with a Looper.

    Short example:

    public class MyActivity extends Activity {
        private Handler mHandler;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            HandlerThread handlerThread = new HandlerThread("background-handler");
            handlerThread.start();
            Looper looper = handlerThread.getLooper();
            mHandler = new Handler(looper);
    
            mHandler.post(new Runnable() {
                public void run() {
                    // code executed in handlerThread
                }
            });
        }
        @Override
        protected void onDestroy() {
            super.onDestroy();
            // stops the HandlerThread
            mHandler.getLooper().quit();
        }
    }
    

    In case your task needs only a some information and does not need to report back, I’d go with an IntentService. Those don’t go mad if your Activity-lifecycle recreates the Activity.

    You would create a small Service in it’s own file

    public class SaveService extends IntentService {
        public SaveService() {
            super("SaveService");
        }
        @Override
        protected void onHandleIntent(Intent intent) {
            if ("com.example.action.SAVE".equals(intent.getAction())) {
                String player = intent.getStringExtra("com.example.player");
                int score = intent.getIntExtra("com.example.score", -1);
                magicHttpSave(player, score); // assuming there is an implementation here
            }
        }
    }
    

    Add it to the AndroidManifest.xml

    <application ....
    
        <service android:name=".SaveService" />
    </application>
    

    And in your code start it with

    Intent intent = new Intent(this /* context */, SaveService.class);
    intent.setAction("com.example.action.SAVE");
    intent.putExtra("com.example.player", "John123");
    intent.putExtra("com.example.score", 5123);
    startService(intent);
    

    IntentService#onHandleIntent() runs on a background thread already so you don’t have to bother about that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a game years ago (under Windows 95) in Pascal. Since then over
Several years ago, I wrote a small Cocoa/Obj-C game framework for OpenGL ES 1.1
A long time ago (Netscape 4-era), I wrote Javascript-based games: Pong, Minesweeper, and John
I want to write some games, but I don't have any game development experience.
I'm trying to re-implement an old Reversi board game I wrote with a bit
I wrote a game class with coffeescript that displays a plain and a rotating
Back in college I wrote a game where the computer would sleep for 1
I am looking into making a text based game that I wrote in Haskell
I wrote a nice little game which works well in its .fla form. Because
I wrote this as a simple dice game. It works as I want except

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.