源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Android计步模块实例代码(类似微信运动)

  • 时间:2020-08-04 22:01 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Android计步模块实例代码(类似微信运动)
最近在项目中研究计步模块,每天0点开始记录当天的步数,类似微信运动。碰到了不少坑今天有时间整理出来给大家看看。 做之前在google、baidu、github上搜了个遍没找到好的,大多数都是需要在后台存活,需要后台Service。 对于现在的各大手机厂商为了提高电池的续航里程(省电),基本上AlertManager、 android.intent.action.BOOT_COMPLETED、后台Service都是被干掉的。 后台保活策略Service,基本上没什么用,被手机系统干掉只是时间问题,所以我认为最好也不要去做,就算后台存活了,用户看到这个app非常费电也会被删除的。 项目地址:[url=https://github.com/jiahongfei/TodayStepCounter]https://github.com/jiahongfei/TodayStepCounter[/url] [b]目前android计步有两种方式[/b] [b]系统计步芯片[/b] 在Android4.4版本之后,部分机型实现了Sensor.TYPE_STEP_COUNTER传感器,用于纪录用户行走的步数。从手机开机开始纪录,手机关机时重置为0。 这个记步芯片是系统级别的,相对之前老版本的传感器记步,性能有一些优化: 不会因为App单独用了记步的功能而额外耗电 系统芯片记步是持续的,能够优化部分机型后台不记步的问题。 [b]加速度传感器计算方式[/b] 加速度传感器非常耗电,导致App的耗电量很高,影响用户体验。 需要后台实时运行才能实现记步的功能,如果App进程被系统或者安全软件杀死,导致记步功能没办法使用 项目地址:[url=https://github.com/jiahongfei/TodayStepCounter]https://github.com/jiahongfei/TodayStepCounter[/url] 根据以上两种方式实现计步,手机提供计步传感器就使用Sensor.TYPE_STEP_COUNTER方式(app后台关闭也可以计步),如果不提供就使用SensorManager.SENSOR_DELAY_UI方式(app需要保持后台运行)。 [b]项目结构:[/b] 计步Service使用单独进程,所以使用到进程间通信aidl,todaystepcounterlib为库文件用于在单独进程中实现计步算法,app依赖todaystepcounterlib项目获取当前步数展示。 [img]http://files.jb51.net/file_images/article/201707/2017070110541329.png[/img] 接入方式: 项目结构app中时如何使用计步模块的看如下代码
public class MainActivity extends AppCompatActivity {
  private static String TAG = "MainActivity";
  private static final int REFRESH_STEP_WHAT = 0;
  //循环取当前时刻的步数中间的间隔时间
  private long TIME_INTERVAL_REFRESH = 500;
  private Handler mDelayHandler = new Handler(new TodayStepCounterCall());
  private int mStepSum;
  private ISportStepInterface iSportStepInterface;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent intent = new Intent(this, VitalityStepService.class);
    startService(intent);
    bindService(intent, new ServiceConnection() {
      @Override
      public void onServiceConnected(ComponentName name, IBinder service) {
        iSportStepInterface = ISportStepInterface.Stub.asInterface(service);
        try {
          mStepSum = iSportStepInterface.getCurrTimeSportStep();
          updateStepCount();
        } catch (RemoteException e) {
          e.printStackTrace();
        }
     mDelayHandler.sendEmptyMessageDelayed(REFRESH_STEP_WHAT, TIME_INTERVAL_REFRESH);
      }
      @Override
      public void onServiceDisconnected(ComponentName name) {
      }
    }, Context.BIND_AUTO_CREATE);
  }
  class TodayStepCounterCall implements Handler.Callback{
    @Override
    public boolean handleMessage(Message msg) {
      switch (msg.what) {
        case REFRESH_STEP_WHAT: {
          if (null != iSportStepInterface) {
            int step = 0;
            try {
              step = iSportStepInterface.getCurrTimeSportStep();
            } catch (RemoteException e) {
              e.printStackTrace();
            }
            if (mStepSum != step) {
              mStepSum = step;
              updateStepCount();
            }
          }
     mDelayHandler.sendEmptyMessageDelayed(REFRESH_STEP_WHAT, TIME_INTERVAL_REFRESH);
          break;
        }
      }
      return false;
    }
  }
  private void updateStepCount() {
    Log.e(TAG,"updateStepCount : " + mStepSum);
    TextView stepTextView = (TextView)findViewById(R.id.stepTextView);
    stepTextView.setText(mStepSum + "步");
  }
}
[b]计步策略:[/b] 1.如果使用加速度传感器计步必须要app在后台存活才可以计步。‘ 2.重头戏是使用计步传感器实现计步,app在后台关闭也可以计步。 [b]如下是采用Sensor.TYPE_STEP_COUNTER传感器实现计步策略:[/b] 1. 用户新安装app,从用户第一次打开App开始计步,当天不跨天 [img]http://files.jb51.net/file_images/article/201707/2017070110541330.png[/img] 2. 用户一直打开app计步,且跨越0点没有关闭App [img]http://files.jb51.net/file_images/article/201707/2017070110541331.png[/img] 3.用户打开一次App后台关闭,跨越0点且0点分隔AlertManager不能自启动(目前多数手机都是不能启动的) [img]http://files.jb51.net/file_images/article/201707/2017070110541332.png[/img] 4.用户打开一次app后台关闭,跨越多个0点且Alertmanager 0点分隔可以启动 [img]http://files.jb51.net/file_images/article/201707/2017070110541433.png[/img] 5.用户开启一次app且在同一天进行重启手机(自启动不好用,很多手机不好用) [img]http://files.jb51.net/file_images/article/201707/2017070110541434.png[/img] 6.用户开启一次app,开关机跨0点(开机自启动不好用) [img]http://files.jb51.net/file_images/article/201707/2017070110541435.png[/img] 7.用户开启一次app,开关机跨0点(开机自启动可以) [img]http://files.jb51.net/file_images/article/201707/2017070110541436.png[/img] [b]缺陷[/b] 1.方案三 跨0点打开app步数算 前一天的,如果跨越多天会导致前一天步数非常大。 2.方案四 跨0点之前的步数会丢失(由于0点分隔AlertManager可以回调,所以可以处理0点之前的数据,以后版本在修复吧) 3.在计步器回调中频繁调用SharePreference费电 [b]注意:[/b] 1.每天早上打开App可以提高几步精度,和微信步数几乎一致。 2.每次重启手机请打开app,会合并步数 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程素材网。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部