1

以下示例取自互联网。在所有示例中,代码都是使用传感器管理器的单独线程来解释的。您能否让我知道为什么所有加速度计都用 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];
}

}
4

1 回答 1

2

因为 run() 方法中的连续 while 循环会阻塞主线程并导致您的应用程序因“应用程序无响应”或 ANR 而关闭。

任何时候你有一个像这样长时间运行的循环,它必须发生在一个单独的线程中。

于 2013-01-31T04:29:30.663 回答