today, let's not talk about the pedometer (pedometer) Project UI, today is about a step-up function sensor based on the gravitational acceleration (sensor), then
Start the step in the background.
Pedometer (pedometer) the entire project source code, interested friends can download to see (remember to help younger brother on GitHub to play a Star ~)
Https://github.com/296777513/pedometer
First a few: (The effect and the previous article about the Circlebar very similar, because the step function in the background)
, can be based on some of your basic parameters, to remember steps. There is a disadvantage, because this is based on the induction of acceleration to calculate whether to take a step, so you are in situ dangling mobile phone, will also remember steps, but normal walking is quite accurate.
First, the code for the Stepdetector class is given:
Package Com.example.histogram.widet;import Android.content.context;import Android.hardware.sensor;import Android.hardware.sensorevent;import Android.hardware.sensoreventlistener;import android.hardware.SensorManager;/ * * This is a step-up class that implements the signal listener * This is a step-up algorithm from Google, see not very understand * @author Liyachao date:2015-1-6 * */public class Stepdetector implements Sen Soreventlistener {public static int current_setp = 0;public static float sensitivity = ten;//sensitivity sensitivity private float mlastvalues[] = new float[3 * 2];p rivate float mscale[] = new Float[2];p rivate float myoffset;private static long end = 0; private static Long start = 0;/** * Last acceleration Direction */private float mlastdirections[] = new float[3 * 2];p rivate float Mlastextrem Es[][] = {new float[3 * 2], new FLOAT[3 * 2]};p rivate float mlastdiff[] = new float[3 * 2];p rivate int mlastmatch = -1;/ * * Constructor for incoming context * * @param context */public Stepdetector (context context) {super (); int h = 480;myoffset = h * 0.5f;mscale [0] =-(H * 0.5f * (1.0f/(Sensormanager.standard_GRAVITY * 2))); mscale[1] =-(H * 0.5f * (1.0f/(Sensormanager.magnetic_field_earth_max)));} This method is called when the sensor detects a change in the value of public void onsensorchanged (Sensorevent event) {Sensor sensor = event.sensor;synchronized ( This) {if (sensor.gettype () = = Sensor.type_accelerometer) {Float vSum = 0;for (int i = 0; i < 3; i++) {final float v = Myoffset + event.values[i] * mscale[1];vsum + = V;} int k = 0;float v = vsum/3;float Direction = (V > mlastvalues[k]? 1: (V < mlastvalues[k]? -1:0)); if (direction = =-mlastdirections[k]) {//Direction changedint Exttype = (Direction > 0? 0:1);//Minumum or//Maximum?mlastextre MES[EXTTYPE][K] = mlastvalues[k];float diff = math.abs (mlastextremes[exttype][k]-mlastextremes[1-exttype][k]); if ( diff > sensitivity) {boolean isalmostaslargeasprevious = diff > (mlastdiff[k] * 2/3); Boolean Ispreviouslargeenough = Mlastdiff[k] > (DIFF/3); Boolean Isnotcontra = (mlastmatch! = 1-exttype); if (isalmostaslargeasprevious && Ispreviouslargeenough&& Isnotcontra) {end = System.currenttimemillis (), if (End-start > 500) {//At this time judged to have gone one step current_setp++;ml Astmatch = Exttype;start = End;}} else {mlastmatch =-1;}} Mlastdiff[k] = diff;} MLASTDIRECTIONS[K] = direction;mlastvalues[k] = V;}}} This method is called when the longitude of the sensor changes, not in the public void onaccuracychanged (sensor arg0, int arg1) {}}
Down is the code for the backend service Stepservice:
Package Com.example.histogram.widet;import Android.app.service;import Android.content.context;import Android.content.intent;import Android.hardware.sensor;import Android.hardware.sensormanager;import Android.os.ibinder;public class Stepservice extends Service {public static Boolean flag = False;private Sensormanager sens Ormanager;private stepdetector stepdetector; @Overridepublic ibinder onbind (Intent arg0) {//TODO auto-generated method Stubreturn null;} @Overridepublic void OnCreate () {super.oncreate ();//This opens a thread because the background service is also in the main thread, which is a safe point to prevent the main thread from blocking the new thread (new Runnable () {public void run () {Startstepdetector ()}}). Start ();} private void Startstepdetector () {flag = True;stepdetector = new Stepdetector (this), Sensormanager = (Sensormanager) this. Getsystemservice (Sensor_service);//Get the Sensor Manager Instance sensor sensor = Sensormanager.getdefaultsensor (sensor.type_ ACCELEROMETER);//To obtain the type of sensor, the type obtained here is the accelerometer///This method is used to register, only the registration will take effect, parameters: sensoreventlistener instance, sensor instance, Update Rate Sensormanager.registerlistener (Stepdetector, Sensor,sensormanager.sensor_delay_fastest);} @Overridepublic int Onstartcommand (Intent Intent, int flags, int startid) {return Super.onstartcommand (Intent, Flags, STA Rtid);} @Overridepublic void OnDestroy () {Super.ondestroy (); flag = False;if (stepdetector! = null) { Sensormanager.unregisterlistener (Stepdetector);}}}
Finally put the Fragmentpedometer test page code also to everyone, if you have seen before the blog, you should know that this is a fragment. If you have not seen it, see your interested friends at the previous blog post:
Package Com.example.histogram;import Java.text.simpledateformat;import Java.util.date;import Com.example.changepage1.r;import Com.example.histogram.widet.circlebar;import Com.example.histogram.widet.stepdetector;import Com.example.histogram.widet.stepservice;import Com.example.histogram.widet.weather;import Android.annotation.suppresslint;import Android.content.Intent;import Android.os.bundle;import Android.os.handler;import Android.os.message;import android.support.v4.app.Fragment; Import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import android.view.view.onclicklistener;/** * This is the fragment of the step * Author: Li Wu Super email:[email protected] * date:2015-1-6 * Time: PM 8: */public class Fragmentpedometer extends Fragment implements Onclicklistener{private View view;private Circlebar CIRCL ebar;private int total_step = 0;private Thread thread;private int Type = 1;private int calories = 0;private int Step_lengt h = 50;private int weight = 70;private Weather weatHer;private String test;private Boolean flag = true;//to determine whether the third page turns on animation @suppresslint ("Handlerleak") Handler Handler = new Ha Ndler () {public void Handlemessage (Message msg) {super.handlemessage (msg); total_step = Stepdetector.current_setp;if ( Type = = 1) {circlebar.setprogress (total_step, type);} else if (Type = = 2) {calories = (int) (weight * Total_step * step_le Ngth * 0.01 * 0.01); circlebar.setprogress (calories, Type);} else if (Type = = 3) {if (flag) {circlebar.startcustomanimation (); flag = false;} if (test! = NULL | | weather.getweather () = null) {Weather.setweather ("in update ..."); Weather.setptime (""); WEATHER.SETTEMP1 (""); WEATHER.SETTEMP2 (""); Circlebar.startcustomanimation (); Circlebar.setweather (weather);} else {circlebar.setweather (weather);}}}; @Overridepublic View Oncreateview (Layoutinflater inflater, ViewGroup container,bundle savedinstancestate) {view = Inflater.inflate (R.layout.pedometer, container, false); init (); Mthread (); return view;} private void Init () {Intent Intent = new Intent (getActivity (), stepservice.class); Getactivity (). StartService (intent); weather = new weather (); Circlebar = (Circlebar) View.findviewbyid (R.id.progress_pedometer); Circlebar.setmax (10000); Circlebar.setprogress (StepDetector.CURRENT_ SETP, 1); Circlebar.startcustomanimation (); Circlebar.setonclicklistener (this);} private void Mthread () {if (thread = = null) {thread = new thread (new Runnable () {public void run () {while (true) {try {Thr Ead.sleep (500);} catch (Interruptedexception e) {e.printstacktrace ();} if (stepservice.flag) {message msg = new Message (); Handler.sendmessage (msg);}}}); Thread.Start ();}} @Overridepublic void OnClick (View v) {switch (V.getid ()) {case R.id.progress_pedometer:if (type = = 1) {type = 2;} else if (Type = = 2) {flag = true; Type = 3;} else if (type = = 3) {type = 1;} Message msg = new Message (); Handler.sendmessage (msg); break;default:break;}}}These are the main code, the other secondary I do not post out, if you want to demo friends can leave a message below, I give you.
If you have any suggestions, you can ask.
Android-based pedometer (pedometer) (four)--backstage step