Android启动非常慢,因为onSensorChanged在onCreate方法中运行了多次

Android启动非常慢,因为onSensorChanged在onCreate方法中运行了多次,android,performance,android-sensors,android-launcher,Android,Performance,Android Sensors,Android Launcher,我正在实现一个AR应用程序演示,它可以工作。但我发现这个应用程序启动非常慢,我做了一些调试,发现可能是“onSensorChanged”导致的,它运行了很多次 有人能帮我吗?提前谢谢 这是密码 package com.example.mazhi_000.cameraapplication; import android.app.Activity; import android.hardware.Sensor; import android.hardware.SensorEvent; impor

我正在实现一个AR应用程序演示,它可以工作。但我发现这个应用程序启动非常慢,我做了一些调试,发现可能是“onSensorChanged”导致的,它运行了很多次

有人能帮我吗?提前谢谢

这是密码

package com.example.mazhi_000.cameraapplication;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.view.SurfaceView;
import android.view.SurfaceHolder;
import android.view.Menu;
import android.view.MenuItem;
import android.hardware.Camera;
import android.util.Log;
import android.widget.TextView;


public class CameraActivity extends Activity {

    SurfaceView cameraPreview;
    SurfaceHolder previewHolder;
    Camera camera;
    boolean inPreview;

    final static String TAG = "CameraSurfaceView";
    SensorManager sensorManager;

    int orientationSensor;
    float headingAngle;
    float pitchAngle;
    float rollAngle;

    int accelerometrSensor;
    float xAxis;
    float yAxis;
    float zAxis;

    LocationManager locationManager;
    double latitude;
    double longitude;
    double altitude;

    TextView xAxisValue;
    TextView yAxisValue;
    TextView zAxisValue;
    TextView headingValue;
    TextView pitchValue;
    TextView rollValue;
    TextView altitudeValue;
    TextView latitudeValue;
    TextView longitudeValue;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);

        // cache textview
        xAxisValue = (TextView) findViewById(R.id.xAxisValue);
        yAxisValue = (TextView) findViewById(R.id.yAxisValue);
        zAxisValue = (TextView) findViewById(R.id.zAxisValue);
        headingValue = (TextView) findViewById(R.id.headingValue);
        pitchValue = (TextView) findViewById(R.id.pitchValue);
        rollValue = (TextView) findViewById(R.id.rollValue);
        altitudeValue = (TextView) findViewById(R.id.altitudeValue);
        longitudeValue = (TextView) findViewById(R.id.longitudeValue);
        latitudeValue = (TextView) findViewById(R.id.latitudeValue);

        locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
        Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if (location != null) {
            latitude = location.getLatitude();
            longitude = location.getLongitude();
            altitude = location.getAltitude();

            Log.d(TAG, "Latitude: " + String.valueOf(latitude));
            Log.d(TAG, "longitude: " + String.valueOf(longitude));
            Log.d(TAG, "Altitude: " + String.valueOf(altitude));

            latitudeValue.setText(String.valueOf(latitude));
            longitudeValue.setText(String.valueOf(longitude));
            altitudeValue.setText(String.valueOf(altitude));

        }
        locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER, 2000, 2, locationListener);

        sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
        orientationSensor = Sensor.TYPE_ORIENTATION;
        accelerometrSensor = Sensor.TYPE_ACCELEROMETER;

        sensorManager.registerListener(sensorEventListener, sensorManager.getDefaultSensor(orientationSensor), SensorManager.SENSOR_DELAY_NORMAL);
        sensorManager.registerListener(sensorEventListener, sensorManager.getDefaultSensor(accelerometrSensor),SensorManager.SENSOR_DELAY_NORMAL);

        inPreview = false;

        cameraPreview = (SurfaceView)findViewById(R.id.cameraPreview);
        previewHolder = cameraPreview.getHolder();
        previewHolder.addCallback(surfaceCallback);
        previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    LocationListener locationListener = new LocationListener()
    {
        @Override
        public void onLocationChanged(Location location) {
            latitude = location.getLatitude();
            longitude = location.getLongitude();
            altitude = location.getAltitude();

            Log.d(TAG, "Latitude: " + String.valueOf(latitude));
            Log.d(TAG, "longitude: " + String.valueOf(longitude));
            Log.d(TAG, "Altitude: " + String.valueOf(altitude));

            latitudeValue.setText(String.valueOf(latitude));
            longitudeValue.setText(String.valueOf(longitude));
            altitudeValue.setText(String.valueOf(altitude));
        }

        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {

        }

        @Override
        public void onProviderEnabled(String s) {

        }

        @Override
        public void onProviderDisabled(String s) {

        }
    };

    final SensorEventListener sensorEventListener = new SensorEventListener()
    {
        @Override
        public void onSensorChanged(SensorEvent sensorEvent)
        {
            if(sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION) {
                headingAngle = sensorEvent.values[0];
                pitchAngle = sensorEvent.values[1];
                rollAngle = sensorEvent.values[2];

                Log.d(TAG, "headingAngle: " + String.valueOf(headingAngle));
                Log.d(TAG, "pitchAngle: " + String.valueOf(pitchAngle));
                Log.d(TAG, "rollAngle: " + String.valueOf(rollAngle));

                headingValue.setText(String.valueOf(headingAngle));
                pitchValue.setText(String.valueOf(pitchAngle));
                rollValue.setText(String.valueOf(rollAngle));
            }
            else
            {
                if(sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
                {
                    xAxis = sensorEvent.values[0];
                    yAxis = sensorEvent.values[1];
                    zAxis = sensorEvent.values[2];

                    Log.d(TAG, "xAxis: " + String.valueOf(xAxis));
                    Log.d(TAG, "yAxis: " + String.valueOf(yAxis));
                    Log.d(TAG, "zAxis: " + String.valueOf(zAxis));

                    xAxisValue.setText(String.valueOf(xAxis));
                    yAxisValue.setText(String.valueOf(yAxis));
                    zAxisValue.setText(String.valueOf(zAxis));
                }
            }
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int i)
        {
            // not used
        }
    };

    SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback()
    {
        public void surfaceCreated(SurfaceHolder holder)
        {
            try
            {
                camera.setPreviewDisplay(previewHolder);
            }
            catch (Throwable t)
            {
                Log.e("ProAndroidAR2Activity", "Exception in setPreviewDisplay()", t);
            }
        }

        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
        {
            Camera.Parameters parameters = camera.getParameters();
            Camera.Size size = getBestPreviewSize(width, height, parameters);

            if(size != null)
            {
                parameters.setPreviewSize(size.width, size.height);
                camera.setParameters(parameters);
                camera.startPreview();
                inPreview = true;
            }
        }

        public  void  surfaceDestroyed(SurfaceHolder holder)
        {
            // not userd
//            camera.stopPreview();
//            camera.release();
//            camera = null;
        }
    };

    private Camera.Size getBestPreviewSize(int width, int height, Camera.Parameters parameters)
    {
        Camera.Size result=null;
        //Camera.Parameters p = parameters;
        for (Camera.Size size : parameters.getSupportedPreviewSizes())
        {
            if (size.width <= width && size.height <= height)
            {
                if (result==null)
                {
                    result=size;
                }
                else
                {
                    int resultArea = result.width * result.height;
                    int newArea = size.width * size.height;

                    if (newArea>resultArea) {
                        result=size;
                    }
                }
            }
        }
        return result;
    }

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

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 2, locationListener);
        sensorManager.registerListener(sensorEventListener, sensorManager.getDefaultSensor(orientationSensor), SensorManager.SENSOR_DELAY_NORMAL);
        sensorManager.registerListener(sensorEventListener, sensorManager.getDefaultSensor(accelerometrSensor), SensorManager.SENSOR_DELAY_NORMAL);

        camera = Camera.open();
    }

    @Override
    public  void onPause()
    {
        if(inPreview)
        {
            camera.stopPreview();
        }

        locationManager.removeUpdates(locationListener);
        sensorManager.unregisterListener(sensorEventListener);

        camera.release();
        camera = null;
        inPreview = false;

        super.onPause();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.camera, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
package com.example.mazhi_000.camera应用程序;
导入android.app.Activity;
导入android.hardware.Sensor;
导入android.hardware.SensorEvent;
导入android.hardware.SensorEventListener;
导入android.hardware.SensorManager;
导入android.location.location;
导入android.location.LocationListener;
导入android.location.LocationManager;
导入android.os.Bundle;
导入android.os.Handler;
导入android.view.SurfaceView;
导入android.view.SurfaceHolder;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.hardware.Camera;
导入android.util.Log;
导入android.widget.TextView;
公共类CameraActivity扩展了活动{
表面审查;
表面活性物质;
摄像机;
回顾;
最终静态字符串标记=“CameraSurfaceView”;
传感器管理器传感器管理器;
内方位传感器;
浮头角;
浮动俯仰角;
浮动滚转角;
加速度传感器;
浮动X轴;
浮动yAxis;
浮动zAxis;
地点经理地点经理;
双纬度;
双经度;
双海拔;
文本视图值;
文本视图值;
文本视图值;
文本视图标题值;
文本视图值;
文本视图值;
文本视图高度值;
文本视图纬度值;
文本视图纵向值;
@凌驾
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_摄像头);
//缓存文本视图
xAxisValue=(TextView)findViewById(R.id.xAxisValue);
yAxisValue=(TextView)findViewById(R.id.yAxisValue);
zAxisValue=(TextView)findViewById(R.id.zAxisValue);
headingValue=(TextView)findViewById(R.id.headingValue);
pitchValue=(TextView)findViewById(R.id.pitchValue);
rollValue=(TextView)findViewById(R.id.rollValue);
altitudeValue=(TextView)findViewById(R.id.altitudeValue);
longitudeValue=(TextView)findViewById(R.id.longitudeValue);
latitudeValue=(TextView)findViewById(R.id.latitudeValue);
locationManager=(locationManager)getSystemService(位置服务);
Location Location=locationManager.getLastKnownLocation(locationManager.NETWORK\u提供程序);
如果(位置!=null){
纬度=位置。getLatitude();
longitude=location.getLongitude();
高度=位置。getAltitude();
Log.d(标签,“纬度:”+String.valueOf(纬度));
Log.d(标记“经度:”+String.valueOf(经度));
Log.d(标签,“高度:+String.valueOf(高度));
latitudeValue.setText(String.valueOf(latitude));
longitudeValue.setText(字符串.valueOf(经度));
altitudeValue.setText(字符串.valueOf(高度));
}
locationManager.requestLocationUpdates(locationManager.GPS_提供程序,2000,2,locationListener);
sensorManager=(sensorManager)getSystemService(传感器服务);
方向传感器=传感器类型\方向;
加速度计传感器=传感器类型\加速计;
sensorManager.registerListener(sensorEventListener、sensorManager.getDefaultSensor(方向传感器)、sensorManager.SENSOR\u DELAY\u NORMAL);
sensorManager.registerListener(sensorEventListener,sensorManager.getDefaultSensor(加速度计传感器),sensorManager.SENSOR\u DELAY\u NORMAL);
inPreview=false;
cameraPreview=(SurfaceView)findViewById(R.id.cameraPreview);
previewHolder=cameraPreview.getHolder();
previewHolder.addCallback(surfaceCallback);
previewHolder.setType(SurfaceHolder.SURFACE类型推送缓冲区);
}
LocationListener LocationListener=新LocationListener()
{
@凌驾
已更改位置上的公共无效(位置){
纬度=位置。getLatitude();
longitude=location.getLongitude();
高度=位置。getAltitude();
Log.d(标签,“纬度:”+String.valueOf(纬度));
Log.d(标记“经度:”+String.valueOf(经度));
Log.d(标签,“高度:+String.valueOf(高度));
latitudeValue.setText(String.valueOf(latitude));
longitudeValue.setText(字符串.valueOf(经度));
altitudeValue.setText(字符串.valueOf(高度));
}
@凌驾
状态已更改的公共void(字符串s、int i、Bundle){
}
@凌驾
已提供已启用的公共void(字符串s){
}
@凌驾
公共无效onProviderDisabled(字符串s){
}
};
最终SensorEventListener SensorEventListener=新的SensorEventListener()
{
@凌驾
传感器更改时的公共无效(传感器事件传感器事件)
{
if(sensorEvent.sensor.getType()==sensor.TYPE\u方向){
headingAngle=sensorEvent.values[0];
俯仰角=传感器事件值[1];
rollAngle=sensorEvent.Value[2];
Log.d(标记“headingAngle:+String.valueOf(headingAngle));
Log.d(标记“pitchAngle:”+字符串.valueOf(pitchAngle));
Log.d(标记“rollAngle:+String.valueOf(rollAngle));
headingValue.setText(String.valueOf(headingAngle));
pitchValue.setText(String.valueOf(pitchAngle));
rollValue.setText(String.valueOf(rollAngle));
}
其他的
{
if(sensorEvent.sensor.getType()==sensor.TYPE\u加速计
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <SurfaceView
        android:id="@+id/cameraPreview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
    <TextView
        android:id="@+id/xAxisLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="18dp"
        android:layout_marginTop="15dp"
        android:text="@string/xAxis" />
    <TextView
        android:id="@+id/yAxisLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/xAxisLabel"
        android:layout_below="@+id/xAxisLabel"
        android:text="@string/yAxis" />
    <TextView
        android:id="@+id/zAxisLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/yAxisLabel"
        android:layout_below="@+id/yAxisLabel"
        android:text="@string/zAxis" />
    <TextView
        android:id="@+id/headingLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/zAxisLabel"
        android:layout_below="@+id/zAxisLabel"
        android:layout_marginTop="19dp"
        android:text="@string/heading" />
    <TextView
        android:id="@+id/pitchLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/headingLabel"
        android:layout_below="@+id/headingLabel"
        android:text="@string/pitch" />
    <TextView
        android:id="@+id/rollLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/pitchLabel"
        android:layout_below="@+id/pitchLabel"
        android:text="@string/roll" />
    <TextView
        android:id="@+id/latitudeLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/rollLabel"
        android:layout_below="@+id/rollLabel"
        android:layout_marginTop="34dp"
        android:text="@string/latitude" />
    <TextView
        android:id="@+id/longitudeLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/latitudeLabel"
        android:layout_below="@+id/latitudeLabel"
        android:text="@string/longitude" />
    <TextView
        android:id="@+id/altitudeLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/longitudeLabel"
        android:layout_below="@+id/longitudeLabel"
        android:text="@string/altitude" />
    <TextView
        android:id="@+id/xAxisValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/xAxisLabel"
        android:layout_marginLeft="56dp"
        android:layout_toRightOf="@+id/longitudeLabel"
        android:text="@string/empty" />
    <TextView
        android:id="@+id/yAxisValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/yAxisLabel"
        android:layout_alignBottom="@+id/yAxisLabel"
        android:layout_alignLeft="@+id/xAxisValue"
        android:text="@string/empty" />
    <TextView
        android:id="@+id/zAxisValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/headingLabel"
        android:layout_alignLeft="@+id/yAxisValue"
        android:text="@string/empty" />
    <TextView
        android:id="@+id/headingValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/headingLabel"
        android:layout_alignBottom="@+id/headingLabel"
        android:layout_alignLeft="@+id/zAxisValue"
        android:text="@string/empty" />
    <TextView
        android:id="@+id/pitchValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/pitchLabel"
        android:layout_alignBottom="@+id/pitchLabel"
        android:layout_alignLeft="@+id/headingValue"
        android:text="@string/empty" />
    <TextView
        android:id="@+id/rollValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/latitudeLabel"
        android:layout_alignLeft="@+id/pitchValue"
        android:text="@string/empty" />
    <TextView
        android:id="@+id/latitudeValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/latitudeLabel"
        android:layout_alignLeft="@+id/rollValue"
        android:text="@string/empty" />
    <TextView
        android:id="@+id/longitudeValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/longitudeLabel"
        android:layout_alignBottom="@+id/longitudeLabel"
        android:layout_alignLeft="@+id/latitudeValue"
        android:text="@string/empty" />
    <TextView
        android:id="@+id/altitudeValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/altitudeLabel"
        android:layout_alignBottom="@+id/altitudeLabel"
        android:layout_alignLeft="@+id/longitudeValue"
        android:text="@string/empty" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mazhi_000.cameraapplication" >

    <uses-sdk android:minSdkVersion="7"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".CameraActivity"
            android:screenOrientation = "landscape"
            android:configChanges="keyboardHidden|orientation"
            android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"

            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

    <uses-feature android:name="android.hardware.camera"/>
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

</manifest>
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 2, locationListener);
sensorManager.registerListener(sensorEventListener, sensorManager.getDefaultSensor(orientationSensor), SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(sensorEventListener, sensorManager.getDefaultSensor(accelerometrSensor), SensorManager.SENSOR_DELAY_NORMAL);