简体   繁体   English

由于onSensorChanged在onCreate方法中运行了很多次,因此Android的启动速度非常慢

[英]Android launch very slow since onSensorChanged runs many times in onCreate method

I am implementing a AR app demo and it can work. 我正在实施一个AR应用程序演示,它可以正常工作。 but i found that this app launch very slow, i did some debugging and found that it may be caused by "onSensorChanged" which runed many times. 但是我发现此应用程序启动非常慢,我进行了一些调试,发现它可能是由运行了多次的“ onSensorChanged”引起的。

Does anyone can help me out? 有人可以帮我吗? thanks in advance! 提前致谢!

here is the code! 这是代码!

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

layout: 布局:

<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>

Manifest 表现

<?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>

Your code looks OK except you do not need 您的代码看起来不错,除非您不需要

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);

in onCreate, because you have it onResume. 在onCreate中,因为您具有onResume。 Also onSensorChanged is supposed to run many times:) try to set some fillers... 另外onSensorChanged应该运行很多次:)尝试设置一些填充物...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM