三星S3使用SensorManager崩溃

时间:2015-01-15 13:12:54

标签: crash android-sensors samsung-mobile acceleration proximitysensor

目前我正在学习在Android开发中使用SensorManager,但每次我在三星Galaxy S3上测试它时,应用程序崩溃,我必须断开连接并重新启动手机。屏幕旋转已启用,之前我对其他程序没有任何问题。它在我尝试使用SensorManager时开始。

这是我尝试测试的程序:

import java.math.BigDecimal;
import android.app.Activity;
import android.os.Bundle;
import android.content.*;
import android.content.res.*;
import android.graphics.*;
import android.view.*;
import android.content.pm.ActivityInfo;
import android.renderscript.*;
import android.hardware.*;

public class Game extends Activity implements SensorEventListener {
DrawView drawView;
SensorManager sensors;
Sensor linear=null;
String title="Linear Acceleration Sensor Demo";
Float3 data = new Float3(0,0,0);
Float3 min = new Float3(0,0,0);
Float3 max = new Float3(0,0,0);

@Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    drawView = new DrawView(this);
    setContentView(drawView);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    //initialize sensors
    sensors = (SensorManager)getSystemService(SENSOR_SERVICE);

    //try to get linear acceleration sensor
    linear = sensors.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
    if (linear==null)
        title = "ERROR: NO LINEAR ACCELERATION SENSOR WAS FOUND";
}

@Override public void onSensorChanged(SensorEvent event) {
    switch (event.sensor.getType()) {
    case Sensor.TYPE_LINEAR_ACCELERATION:

        //get X value and save limits
        data.x = event.values[0];
        if (data.x < min.x) min.x = data.x;
        if (data.x > max.x) max.x = data.x;

        //get Y value and save limits
        data.y = event.values[1];
        if (data.y < min.y) min.y = data.y;
        if (data.y > max.y) max.y = data.y;
        break;
    }
}

@Override public void onAccuracyChanged(Sensor arg0, int arg1) {
}

@Override public void onResume() {
    super.onResume();
    drawView.resume();

    //enable the sensor
    sensors.registerListener(this, linear, SensorManager.
        SENSOR_DELAY_NORMAL);
}

@Override public void onPause() {
    super.onPause();
    drawView.pause();

    //disable the sensor
    sensors.unregisterListener(this);
}

public class DrawView extends SurfaceView implements Runnable 
{
    Thread gameloop = null; 
    SurfaceHolder surface = null; 
    volatile boolean running = false; 
    AssetManager assets = null; 
    Paint paint = new Paint();
    Float2 center;

    //constructor method
    public DrawView(Context context) {
        super(context);
        surface = getHolder();
        assets = context.getAssets();
    }

    public void resume() {
        running = true;
        gameloop = new Thread(this);
        gameloop.start();
    }

    public void pause() {
        running = false;
        while (true) {
            try { 
                gameloop.join();
            } 
            catch (InterruptedException e) { }
        }
    }

    //thread run method
    @Override public void run() {

        while (running) {
            if (!surface.getSurface().isValid()) continue;

            //open the canvas for drawing
            Canvas canvas = surface.lockCanvas();

            //clear the screen 
            canvas.drawColor(Color.BLACK);

            int centerx = canvas.getWidth()/2;
            int centery = canvas.getHeight()/2;
            int top = centery-100;
            int bottom = centery+100;

            paint.setColor(Color.WHITE);
            canvas.drawLine(centerx, top, centerx, bottom, paint);

            int minColor = Color.argb(100, 200, 0, 0);
            int maxColor = Color.argb(100, 0, 200, 0);

            //draw bar for -X minimum (RIGHT)
            int range = (int)Math.abs(min.x)*10;
            Rect rect = new Rect(centerx, centery-30, centerx+range, 
                    centery+30);
            paint.setColor(minColor);
            canvas.drawRect(rect, paint);

            //draw bar for +X maximum (LEFT)
            range = (int)Math.abs(max.x)*10;
            rect = new Rect(centerx-range, centery-30, centerx, 
                    centery+30);
            paint.setColor(maxColor);
            canvas.drawRect(rect, paint);

            //draw bar for -Y minimum (UP)
            range = (int)Math.abs(min.y)*10;
            rect = new Rect(centerx-30, centery-range, centerx+30, 
                    centery);
            paint.setColor(minColor);
            canvas.drawRect(rect, paint);

            //draw bar for +Y maximum (DOWN)
            range = (int)Math.abs(max.y)*10;
            rect = new Rect(centerx-30, centery, centerx+30, 
                    centery+range);
            paint.setColor(maxColor);
            canvas.drawRect(rect, paint);

            //draw bar for ACTUAL Y
            range = (int)data.y*10;
            if (range < 0) {
                //range is negative here
                rect = new Rect(centerx-30, centery+range, centerx+30, 
                        centery);
                paint.setColor(Color.RED);
            }
            else if (range > 0) {
                //range is positive here
                rect = new Rect(centerx-30, centery, centerx+30, 
                        centery+range);
                paint.setColor(Color.GREEN);
            }
            canvas.drawRect(rect, paint);

            //draw bar for ACTUAL X
            range = (int)data.x*10;
            if (range < 0) {
                //range is negative here
                rect = new Rect(centerx, centery-30, centerx-range, 
                        centery+30);
                paint.setColor(Color.RED);
            }
            else if (range > 0) {
                //range is positive here
                rect = new Rect(centerx, centery-30, centerx-range, 
                        centery+30);
                paint.setColor(Color.GREEN);
            }
            canvas.drawRect(rect, paint);

            //display sensor info 
            paint.setColor(Color.WHITE);
            paint.setTextSize(24);
            canvas.drawText(title, 10, 20, paint);
            canvas.drawText("Model: " + linear.getVendor(), 10, 50, 
                    paint);
            canvas.drawText("Data: " + toString(data), 10, 80, paint);
            canvas.drawText("Min: " + toString(min), 10, 110, paint);
            canvas.drawText("Max: " + toString(max), 10, 140, paint);

            //close the canvas
            surface.unlockCanvasAndPost(canvas);

            try {
                Thread.sleep(20);
            }
            catch(InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public String toString(int value) {
        return Integer.toString(value);
    }

    public String toString(Float3 value) {
        String s = "X:" + round(value.x) + "," +
            "Y:" + round(value.y) + "," +
            "Z:" + round(value.z);
        return s;
    }

    //round to a default 2 decimal places
    public double round(double value) {
        return round(value,2);
    }

    //round a number to any number of decimal places
    public double round(double value, int precision) {
        BigDecimal bd = new BigDecimal(value);
        BigDecimal rounded = bd.setScale(precision, 
                BigDecimal.ROUND_HALF_UP);
        return rounded.doubleValue();
    }
}

} 

Min SDK版本为13,因此不应该是问题。

LogCat说:

01-15 13:44:21.549: E/SELinux(7000): Function: selinux_android_load_priority [0], There is no sepolicy file 
01-15 13:44:21.549: E/SELinux(7000):  
01-15 13:44:21.549: E/SELinux(7000): Function: selinux_android_load_priority , loading version is VE=SEPF_GT-I9305_4.3_0021
01-15 13:44:21.549: E/SELinux(7000):  
01-15 13:44:21.549: E/SELinux(7000):  
01-15 13:44:21.549: E/SELinux(7000): selinux_android_seapp_context_reload: seapp_contexts file is loaded from /data/security/spota/seapp_contexts
01-15 13:44:21.554: D/dalvikvm(7000): Late-enabling CheckJNI
01-15 13:44:21.569: I/dalvikvm(7000): Turning on JNI app bug workarounds for target SDK version 13...
01-15 13:44:21.634: W/ActivityThread(7000): Application android.program can be debugged on port 8100...
01-15 13:44:22.134: D/SensorManager(7000): registerListener :: create queue :: handler = 7, name = iNemoEngine Linear Acceleration sensor, delay = 200000, 

之前从未见过这种类型的LogCat,所以我发现很难解读它的含义。这是某种硬件问题吗?

0 个答案:

没有答案