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

  • Home
  • SEARCH
  • 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 9214261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:56:14+00:00 2026-06-18T01:56:14+00:00

The following example is taken from internet. In all the example the code is

  • 0

The following example is taken from internet. In all the example the code is explained using a seprate thread for sensor manager. Could you please let me know why all the accelerometer is explained with a seperate thread for SensorManager

package com.example.myapp1;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.TextView;
import android.widget.Toast;

public class Accelerate extends Activity implements SensorEventListener {

float x, y, sensorX, sensorY;
Bitmap ball;
SensorManager sm;
Sensor s;
MyBringBackSurface ourSurfaceView;


public class MyBringBackSurface extends SurfaceView implements Runnable { 
    SurfaceHolder ourHolder;
    Thread ourThread = null;
    boolean isRunning = false;

    public MyBringBackSurface(Context context) {
        super(context);
        Log.i("Notice","In constructor of mybringbacksurface"); 
        ourHolder = getHolder();
    }

    public void pause() {
        isRunning = false;
        Log.i("Notice","In pause of mybringbacksurface"); 
        while (true) {
            try {
                ourThread.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            break;
        }
        ourThread = null;
    }

    public void resume() {
        Log.i("Notice","In resume of mybringbacksurface"); 
        isRunning = true;
        ourThread = new Thread(this);
        ourThread.start();
    }

    public void run() {
        // TODO Auto-generated method stub
        Log.i("Notice","In run of mybringbacksurface"); 
        while (isRunning) {
            if (!ourHolder.getSurface().isValid())
                continue;

            Canvas canvas = ourHolder.lockCanvas();
            canvas.drawRGB(02, 02, 150);
            float centerX = canvas.getWidth() / 2;
            float centerY = canvas.getHeight() / 2;
            canvas.drawBitmap(ball, centerX - sensorX * 18, centerY
                    + sensorY * 18, null);
            ourHolder.unlockCanvasAndPost(canvas);
        }
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.i("Notice","In oncreate of of Accelerator"); 
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    if (sm.getSensorList(Sensor.TYPE_ACCELEROMETER).size() != 0) {
        s = sm.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
        sm.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL);
    }

    ball = BitmapFactory.decodeResource(getResources(), R.drawable.greenball);
    x = y = sensorX = sensorY = 0;
    ourSurfaceView = new MyBringBackSurface(this);
    ourSurfaceView.resume();
    setContentView(ourSurfaceView);
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    Log.i("Notice","In pause of Accelerator"); 
    sm.unregisterListener(this, s);
    super.onPause();
    ourSurfaceView.pause();
}

public void onAccuracyChanged(Sensor arg0, int arg1) {
    // TODO Auto-generated method stub

}

public void onSensorChanged(SensorEvent e) {
    // TODO Auto-generated method stub
    Log.i("Notice","In sensorchanged of of Accelerator"); 
    try {
        Thread.sleep(32);
    } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    sensorX = e.values[0];
    sensorY = e.values[1];
}

}
  • 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-18T01:56:15+00:00Added an answer on June 18, 2026 at 1:56 am

    Because the continuous while loop in the run() method would block the main thread and cause your app to be shut down with an “Application Not Responding” or ANR.

    Anytime you have a long running loop like that, it must happen in a separate thread.

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

Sidebar

Related Questions

I'm using the following code on my iPhone app, taken from here to extract
I have the following code example taken from the code of a Form :
The following code is taken from the 'JavaScript by Example Second Edition'. I think
I have the following code (taken from facebook-sdk appengine example ): class BaseHandler(webapp2.RequestHandler): @property
Consider the following example taken from http://www.albahari.com/threading/ : using System; using System.Threading; class ThreadTest
Look at the following example (partially taken from MSDN Blog ): class Animal {
The following example is taken from C# in Depth: What you need to master
This may well have come up before but the following code is taken from
The following example is taken from: http://php.net/manual/en/function.curl-multi-close.php#example-3540 This example will create two cURL handles,
I'm following the example of JTreeTable filesystem2 taken from the sun site http://java.sun.com/products/jfc/tsc/articles/treetable2/index.html#updating_the My

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.