参考链接: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 <ScrollVIEw10 androID:layout_wIDth="match_parent"11 androID:layout_height="wrap_content">12 </ScrollVIEw>13 14 <ListVIEw15 androID:ID="@+ID/ListvIEw"16 androID:layout_wIDth="match_parent"17 androID:layout_height="400dp">18 </ListVIEw>19 20 <TextVIEw21 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 //重写构造方法,并设置工厂为null14 public DBOpenHelper(Context context, String name, sqliteDatabase.CursorFactory factory, int version) {15 super(context, name, null, version);16 }17 18 //创建计步表19 @OverrIDe20 public voID onCreate(sqliteDatabase db) {21 db.execsql(CREATE_STEPS);22 }23 24 //重写基类的onUpgrade()方法,以便数据库版本更新25 @OverrIDe26 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>
总结
以上是内存溢出为你收集整理的安卓计步器的简单实现全部内容,希望文章能够帮你解决安卓计步器的简单实现所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)