知識
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價值,我們在追求其視覺表現(xiàn)的同時,更側(cè)重于功能的便捷,營銷的便利,運營的高效,讓網(wǎng)站成為營銷工具,讓軟件能切實提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序為后期升級提供便捷的支持!
基于Android的計步器(Pedometer)的講解(四)——后臺記
發(fā)表時間:2020-10-19
發(fā)布人:葵宇科技
瀏覽次數(shù):39
今天先不說Pedometer(計步器)項目UI方面的了,今天講一個基于重力加快度的記步功能傳感器(Sensor),然后
在后臺開啟記步。
計步器(Pedometer)全部項目標(biāo)源代碼,感興趣的同伙可以下載來看看(記得幫小弟在github打個星~)
https://github.com/296777513/pedometer
先上幾張效不雅圖:(效不雅和上一篇講到的CircleBar異常的相似,因為記步功能在后臺)
[img]http://img.blog.csdn.net/20150106203148421?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYTI5Njc3NzUxMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center[img]http://img.blog.csdn.net/20150106203225891?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYTI5Njc3NzUxMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
如圖所示,能根據(jù)你的一些根本參數(shù),來記步。有一個缺點,因為這個是根據(jù)感應(yīng)加快度來計算是否走一步,所以你在原地晃手機,也會記步,不過正常的走路照樣挺精確的。
起首給出StepDetector類的代碼:
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; /** * 這是一個實現(xiàn)了旌旗燈號監(jiān)聽的記步的類 * 這是大年夜谷歌找來的一個記步的算法,看不太懂 * @author Liyachao Date:2015-1-6 * */ public class StepDetector implements SensorEventListener { public static int CURRENT_SETP = 0; public static float SENSITIVITY = 10; // SENSITIVITY靈敏度 private float mLastValues[] = new float[3 * 2]; private float mScale[] = new float[2]; private float mYOffset; private static long end = 0; private static long start = 0; /** * 最后加快度偏向 */ private float mLastDirections[] = new float[3 * 2]; private float mLastExtremes[][] = { new float[3 * 2], new float[3 * 2] }; private float mLastDiff[] = new float[3 * 2]; private int mLastMatch = -1; /** * 傳入高低文的構(gòu)造函數(shù) * * @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))); } //當(dāng)傳感器檢測到的數(shù)值產(chǎn)生變更時就會調(diào)用這個辦法 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 changed int extType = (direction > 0 ? 0 : 1); // minumum or // maximum? mLastExtremes[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) {// 此時斷定為走了一步 CURRENT_SETP++; mLastMatch = extType; start = end; } } else { mLastMatch = -1; } } mLastDiff[k] = diff; } mLastDirections[k] = direction; mLastValues[k] = v; } } } //當(dāng)傳感器的經(jīng)度產(chǎn)生變更時就會調(diào)用這個辦法,在這瑯綾腔有效 public void onAccuracyChanged(Sensor arg0, int arg1) { } }
下來是后臺辦事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 sensorManager; private StepDetector stepDetector; @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { super.onCreate(); //這里開啟了一個線程,因為后臺辦事也是在主線程中進行,如許可以安然點,防止主線程壅塞 new Thread(new Runnable() { public void run() { startStepDetector(); } }).start(); } private void startStepDetector() { flag = true; stepDetector = new StepDetector(this); sensorManager = (SensorManager) this.getSystemService(SENSOR_SERVICE);//獲取傳感器治理器的實例 Sensor sensor = sensorManager .getDefaultSensor(Sensor.TYPE_ACCELEROMETER);//獲得傳感器的類型,這里獲得的類型是加快度傳感器 //此辦法用來注冊,只有注冊過才會生效,參數(shù):SensorEventListener的實例,Sensor的實例,更新速度 sensorManager.registerListener(stepDetector, sensor, SensorManager.SENSOR_DELAY_FASTEST); } @Override public int onStartCommand(Intent intent, int flags, int startId) { return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super.onDestroy(); flag = false; if (stepDetector != null) { sensorManager.unregisterListener(stepDetector); } } }
最后把FragmentPedometer測試頁面的代碼也給大年夜家,如不雅大年夜家看過之前的博客,應(yīng)當(dāng)知道這是一個碎片。如不雅沒看過,看興趣的同伙可以看看之前的博文:
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; /** * 這是記步的碎片 * Author: 李埡超 email:[email protected] * Date: 2015-1-6 * Time: 下晝8:39 */ public class FragmentPedometer extends Fragment implements OnClickListener{ private View view; private CircleBar circleBar; private int total_step = 0; private Thread thread; private int Type = 1; private int calories = 0; private int step_length = 50; private int weight = 70; private Weather weather; private String test; private boolean flag = true;// 來斷定第三個頁面是否開啟動畫 @SuppressLint("HandlerLeak") Handler handler = new Handler() { 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_length * 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("正在更新中..."); weather.setPtime(""); weather.setTemp1(""); weather.setTemp2(""); circleBar.startCustomAnimation(); circleBar.setWeather(weather); } else { circleBar.setWeather(weather); } } } }; @Override public 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 { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } if (StepService.flag) { Message msg = new Message(); handler.sendMessage(msg); } } } }); thread.start(); } } @Override public 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; } } }這些是重要的代碼,其他次要的我就不貼出來了,如不雅想要demo的同伙可以鄙人面留言,我給你們。
如不雅大年夜家有什么建議,可以提出來哦。
相關(guān)案例查看更多
相關(guān)閱讀
- 云南小程序被騙蔣軍
- 小程序表單
- 昆明軟件定制公司
- 云南網(wǎng)站建設(shè)特性
- 文山小程序開發(fā)
- 昆明小程序公司
- 前端技術(shù)
- 汽車拆解管理系統(tǒng)
- 云南百度小程序
- 云南網(wǎng)站建設(shè)費用
- web開發(fā)技術(shù)
- 云南小程序開發(fā)報價
- 報廢車回收管理軟件
- 英文網(wǎng)站建設(shè)公司
- 云南網(wǎng)絡(luò)推廣
- 迪慶小程序開發(fā)
- web
- 百度小程序開發(fā)公司
- 百度人工排名
- 智慧農(nóng)貿(mào)市場
- 網(wǎng)站開發(fā)
- 云南網(wǎng)站建設(shè)報價
- 云南網(wǎng)站建設(shè)哪家公司好
- 模版消息
- 昆明軟件定制
- 昆明網(wǎng)站設(shè)計
- 汽車報廢回收管理系統(tǒng)
- 云南網(wǎng)站建設(shè)快速優(yōu)化
- 云南旅游網(wǎng)站建設(shè)
- 網(wǎng)頁制作