壮心未与年俱老,死去犹能作鬼雄。这篇文章主要讲述安卓计步器的简单实现相关的知识,希望能为你提供帮助。
参考链接:https://blog.csdn.net/fjnu_se/article/details/90906209
大体思路:在参考链接的基础上,我去掉了显示时间的线程。用android自带计步传感器进行计步,用一个数据库存储每日0点时计步器的总步数,由此可以计算出当日的步数。如果手机重启过,就清除掉所有的旧记录。现有问题是只有在app运行的情况下才能把当日0点时计步器的总步数加入数据库。
运行效果图:
文章图片
结构目录:
文章图片
activity_main.xml:
1 < ?xml version="1.0" encoding="utf-8"?> 2 < RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3xmlns:app="http://schemas.android.com/apk/res-auto" 4xmlns:tools="http://schemas.android.com/tools" 5android:layout_width="match_parent" 6android:layout_height="match_parent" 7tools:context=".MainActivity"> 8 9< ScrollView 10android:layout_width="match_parent" 11android:layout_height="wrap_content"> 12< /ScrollView> 13 14< ListView 15android:id="@+id/listview" 16android:layout_width="match_parent" 17android:layout_height="400dp"> 18< /ListView> 19 20< TextView 21android:id="@+id/steps" 22android:layout_below="@+id/listview" 23android:textSize="25dp" 24android:layout_width="wrap_content" 25android:layout_height="wrap_content" 26android:layout_gravity="center_horizontal" /> 27 28 < /RelativeLayout>
MainActivity:
1 package com.mingrisoft.countsteps; 2 3 import android.content.ContentValues; 4 import android.database.Cursor; 5 import android.database.sqlite.SQLiteDatabase; 6 import android.hardware.Sensor; 7 import android.hardware.SensorEvent; 8 import android.hardware.SensorEventListener; 9 import android.hardware.SensorManager; 10 import android.os.Handler; 11 import android.os.Message; 12 import android.provider.ContactsContract; 13 import android.provider.ContactsContract.Data; 14 import android.provider.FontsContract; 15 import android.sax.EndElementListener; 16 import android.support.v7.app.AppCompatActivity; 17 import android.os.Bundle; 18 import android.text.format.DateFormat; 19 import android.util.Log; 20 import android.widget.TextView; 21 22 import java.text.SimpleDateFormat; 23 import java.util.Date; 24 import java.util.List; 25 26 public class MainActivity extends AppCompatActivity implements SensorEventListener{//不实现SensorEventListener接口在真机上会闪退 27SensorManager mSensorManager; //管理器实例 28Sensor stepCounter; //传感器 29float mSteps; //截止当天0点时步数/重启手机到当前为止的步数 30TextView steps; //显示步数 31TextView time; //显示时间 32private DBOpenHelper dbOpenHelper; //定义DBOpenHelper 33 34@Override 35protected void onCreate(Bundle savedInstanceState) { 36super.onCreate(savedInstanceState); 37setContentView(R.layout.activity_main); 38 39// 获取SensorManager管理器实例 40mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE); 41dbOpenHelper = new DBOpenHelper(MainActivity.this,"db_step",null,1); 42steps = (TextView)findViewById(R.id.steps); 43time = (TextView)findViewById(R.id.time); 44 45// 获取计步器sensor 46stepCounter = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER); 47if(stepCounter != null){ 48// 如果sensor找到,则注册监听器 49mSensorManager.registerListener((SensorEventListener) this,stepCounter,mSensorManager.SENSOR_DELAY_GAME); 50} 51else{ 52Log.e("hemeiwolong","no step counter sensor found"); 53} 54} 55 56//步数变化时 57public void onSensorChanged(SensorEvent event) { 58Cursor cursor = dbOpenHelper.getReadableDatabase().query("tb_step",null,null, null, null,null,null); 59 //while (cursor.moveToNext()) { 60 //Log.i("fffff", cursor.getString(0) + cursor.getString(1) + cursor.getString(2)); 61 //} 62//获取今天的日期,统一格式为yyyy/MM/dd 63String strdate = new SimpleDateFormat("yyyy/MM/dd").format(new Date()); 64 65if (cursor.getCount() == 0) {//如果计步表为空,则把现在为止的总步数加入数据库 66insertData(dbOpenHelper.getReadableDatabase(), strdate, String.valueOf(event.values[0])); 67mSteps = event.values[0]; 68} else { 69if (cursor.moveToLast()) { 70if (event.values[0] < Float.parseFloat(cursor.getString(2))) {//说明手机重启过 71//删除旧的所有步数记录,把现在的总步数加入数据库,注意表名打错的话会闪退 72dbOpenHelper.getReadableDatabase().delete("tb_step",null,null); 73insertData(dbOpenHelper.getReadableDatabase(), strdate, String.valueOf(event.values[0])); 74 75if (cursor.moveToLast()) { 76mSteps = Float.parseFloat(cursor.getString(2)); 77} 78} else {//如果今天没重启过,则取最新的数据作为今天计步的起始值 79mSteps = Float.parseFloat(cursor.getString(2)); 80} 81} 82} 83 84//今天所走步数 = 目前的总步数 - 今天0点为止的总步数,如果今天重启过,只记录重启到现在所走的步数 85float showSteps = event.values[0] - mSteps; 86steps.setText("你已经走了"+String.valueOf((int)showSteps)+"步"); 87 88//到了0点,把到目前为止所走的总步数加入数据库 89String time = "00:00:00"; 90if (time.equals(new SimpleDateFormat("HH:mm:ss").format(new Date()))){ 91insertData(dbOpenHelper.getReadableDatabase(), strdate, String.valueOf(event.values[0])); 92mSteps = event.values[0]; 93} 94} 95 96@Override 97public void onAccuracyChanged(Sensor sensor, int accuracy) { 98 99} 100 101//向数据库添加记录 102private void insertData(SQLiteDatabase sqLiteDatabase, String day, String steps) { 103ContentValues contentValues = new ContentValues(); 104contentValues.put("day", day); 105contentValues.put("steps", steps); 106 107sqLiteDatabase.insert("tb_step",null,contentValues); 108} 109 }
DBOpenHelper:
1 package com.mingrisoft.countsteps; 2 3 import android.content.Context; 4 import android.database.sqlite.SQLiteDatabase; 5 import android.database.sqlite.SQLiteOpenHelper; 6 import android.util.Log; 7 8 public class DBOpenHelper extends SQLiteOpenHelper { 9//定义创建计步表的SQL语句,每列内容分别为序号,日期,步数 10final String CREATE_STEPS = "create table tb_step (_id integer primary key autoincrement," + 11"day, steps)"; 12 13//重写构造方法,并设置工厂为null 14public DBOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { 15super(context, name, null, version); 16} 17 18//创建计步表 19@Override 20public void onCreate(SQLiteDatabase db) { 21db.execSQL(CREATE_STEPS); 22} 23 24//重写基类的onUpgrade()方法,以便数据库版本更新 25@Override 26public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 27Log.i("计步表","版本更新"+oldVersion+"--> "+newVersion); 28} 29 }
manifests:
1 < ?xml version="1.0" encoding="utf-8"?> 2 < manifest xmlns:android="http://schemas.android.com/apk/res/android" 3package="com.mingrisoft.countsteps"> 4 5< uses-feature android:name="android.hardware.sensor.stepcounter"/> 6< uses-feature android:name="feature:android.hardware.sensor.stepdetector"/> 7 8< application 9android:allowBackup="true" 10android:icon="@mipmap/ic_launcher" 11android:label="@string/app_name" 12android:roundIcon="@mipmap/ic_launcher_round" 13android:supportsRtl="true" 14android:theme="@style/AppTheme"> 15< activity android:name=".MainActivity"> 16< intent-filter> 17< action android:name="android.intent.action.MAIN" /> 18 19< category android:name="android.intent.category.LAUNCHER" /> 20< /intent-filter> 21< /activity> 22< /application> 23 24 < /manifest>
【安卓计步器的简单实现】
推荐阅读
- flask 源码专题(app.run()的背后)
- Android_校易app开发日志_第三天
- R switch语句示例图解
- R散点图示例图解
- R repeat循环语句图解实例
- R泊松回归示例图解
- R饼图示例图解
- R正态分布示例图解
- Android Studio学习路程(12)