安卓计步器的简单实现
参考链接: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" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".MainActivity"> 8 9 <ScrollView 10 android:layout_width="match_parent" 11 android:layout_height="wrap_content"> 12 </ScrollView> 13 14 <ListView 15 android:id="@+id/listview" 16 android:layout_width="match_parent" 17 android:layout_height="400dp"> 18 </ListView> 19 20 <TextView 21 android:id="@+id/steps" 22 android:layout_below="@+id/listview" 23 android:textSize="25dp" 24 android:layout_width="wrap_content" 25 android:layout_height="wrap_content" 26 android: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接口在真机上会闪退 27 SensorManager mSensorManager;//管理器实例 28 Sensor stepCounter;//传感器 29 float mSteps;//截止当天0点时步数/重启手机到当前为止的步数 30 TextView steps;//显示步数 31 TextView time;//显示时间 32 private DBOpenHelper dbOpenHelper;//定义DBOpenHelper 33 34 @Override 35 protected void onCreate(Bundle savedInstanceState) { 36 super.onCreate(savedInstanceState); 37 setContentView(R.layout.activity_main); 38 39 // 获取SensorManager管理器实例 40 mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE); 41 dbOpenHelper = new DBOpenHelper(MainActivity.this,"db_step",null,1); 42 steps = (TextView)findViewById(R.id.steps); 43 time = (TextView)findViewById(R.id.time); 44 45 // 获取计步器sensor 46 stepCounter = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER); 47 if(stepCounter != null){ 48 // 如果sensor找到,则注册监听器 49 mSensorManager.registerListener((SensorEventListener) this,stepCounter,mSensorManager.SENSOR_DELAY_GAME); 50 } 51 else{ 52 Log.e("hemeiwolong","no step counter sensor found"); 53 } 54 } 55 56 //步数变化时 57 public void onSensorChanged(SensorEvent event) { 58 Cursor 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 63 String strdate = new SimpleDateFormat("yyyy/MM/dd").format(new Date()); 64 65 if (cursor.getCount() == 0) { //如果计步表为空,则把现在为止的总步数加入数据库 66 insertData(dbOpenHelper.getReadableDatabase(), strdate, String.valueOf(event.values[0])); 67 mSteps = event.values[0]; 68 } else { 69 if (cursor.moveToLast()) { 70 if (event.values[0] < Float.parseFloat(cursor.getString(2))) { //说明手机重启过 71 //删除旧的所有步数记录,把现在的总步数加入数据库,注意表名打错的话会闪退 72 dbOpenHelper.getReadableDatabase().delete("tb_step",null,null); 73 insertData(dbOpenHelper.getReadableDatabase(), strdate, String.valueOf(event.values[0])); 74 75 if (cursor.moveToLast()) { 76 mSteps = Float.parseFloat(cursor.getString(2)); 77 } 78 } else { //如果今天没重启过,则取最新的数据作为今天计步的起始值 79 mSteps = Float.parseFloat(cursor.getString(2)); 80 } 81 } 82 } 83 84 //今天所走步数 = 目前的总步数 - 今天0点为止的总步数,如果今天重启过,只记录重启到现在所走的步数 85 float showSteps = event.values[0] - mSteps; 86 steps.setText("你已经走了"+String.valueOf((int)showSteps)+"步"); 87 88 //到了0点,把到目前为止所走的总步数加入数据库 89 String time = "00:00:00"; 90 if (time.equals(new SimpleDateFormat("HH:mm:ss").format(new Date()))){ 91 insertData(dbOpenHelper.getReadableDatabase(), strdate, String.valueOf(event.values[0])); 92 mSteps = event.values[0]; 93 } 94 } 95 96 @Override 97 public void onAccuracyChanged(Sensor sensor, int accuracy) { 98 99 } 100 101 //向数据库添加记录 102 private void insertData(SQLiteDatabase sqLiteDatabase, String day, String steps) { 103 ContentValues contentValues = new ContentValues(); 104 contentValues.put("day", day); 105 contentValues.put("steps", steps); 106 107 sqLiteDatabase.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语句,每列内容分别为序号,日期,步数 10 final String CREATE_STEPS = "create table tb_step (_id integer primary key autoincrement," + 11 "day, steps)"; 12 13 //重写构造方法,并设置工厂为null 14 public DBOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { 15 super(context, name, null, version); 16 } 17 18 //创建计步表 19 @Override 20 public void onCreate(SQLiteDatabase db) { 21 db.execSQL(CREATE_STEPS); 22 } 23 24 //重写基类的onUpgrade()方法,以便数据库版本更新 25 @Override 26 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 27 Log.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" 3 package="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 9 android:allowBackup="true" 10 android:icon="@mipmap/ic_launcher" 11 android:label="@string/app_name" 12 android:roundIcon="@mipmap/ic_launcher_round" 13 android:supportsRtl="true" 14 android: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>