Friday, 15 May 2015

performance - Android launch very slow since onSensorChanged runs many times in onCreate method -



performance - Android launch very slow since onSensorChanged runs many times in onCreate method -

i implementing ar app demo , can work. found app launch slow, did debugging , found may caused "onsensorchanged" runed many times.

does can help me out? in advance!

here 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; photographic 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(); height = 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(); height = 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) { seek { camera.setpreviewdisplay(previewholder); } grab (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(); // photographic camera = null; } }; private camera.size getbestpreviewsize(int width, int height, camera.parameters parameters) { camera.size result=null; //camera.parameters p = parameters; (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; } } } } homecoming 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); photographic camera = camera.open(); } @override public void onpause() { if(inpreview) { camera.stoppreview(); } locationmanager.removeupdates(locationlistener); sensormanager.unregisterlistener(sensoreventlistener); camera.release(); photographic camera = null; inpreview = false; super.onpause(); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.camera, menu); homecoming true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { homecoming true; } homecoming 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 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 have onresume. onsensorchanged supposed run many times:) seek set fillers...

android performance android-sensors android-launcher

No comments:

Post a Comment