문제

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];
}

}
도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top