byc4426 / TodayStepCounter

Android计步模块(类似微信运动,支付宝计步,今日步数),记录当天从0点到23:59的步数

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Android计步模块(类似微信运动,支付宝计步,今日步数)

介绍请查看简书点击这里

接入方法

1.先下载计步demo TodayStepCounter
2.demo项目结构如下图:
TodayStepCounter项目结构图.png
由图可见todaystepcounterlib是计步模块封装好的Module,它对外提供的接口就是ISportStepInterface.aidl
3.如何接入:
查看对外接口ISportStepInterface.aidl如下代码:

// ISportStepInterface.aidl
package com.today.step.lib;
interface ISportStepInterface {
    /**
     * 获取当前时间运动步数
     */
     int getCurrentTimeSportStep();
     /**
      * 获取当天步数列表,json格式
      */
     String getTodaySportStepArray();
}

查看使用代码MainActivity.java,里面关键代码有注释非常简单

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;
    private TextView mStepArrayTextView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化计步模块
        TodayStepManager.init(getApplication());
        mStepArrayTextView = (TextView)findViewById(R.id.stepArrayTextView);
        //开启计步Service,同时绑定Activity进行aidl通信
        Intent intent = new Intent(this, TodayStepService.class);
        startService(intent);
        bindService(intent, new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                //Activity和Service通过aidl进行通信
                iSportStepInterface = ISportStepInterface.Stub.asInterface(service);
                try {
                    mStepSum = iSportStepInterface.getCurrentTimeSportStep();
                    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: {
                    //每隔500毫秒获取一次计步数据刷新UI
                    if (null != iSportStepInterface) {
                        int step = 0;
                        try {
                            step = iSportStepInterface.getCurrentTimeSportStep();
                        } 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 + "步");
    }
    public void onClick(View view){
        switch (view.getId()){
            case R.id.stepArrayButton:{
                //显示当天计步数据详细,步数对应当前时间
                if(null != iSportStepInterface){
                    try {
                        String stepArray = iSportStepInterface.getTodaySportStepArray();
                        mStepArrayTextView.setText(stepArray);
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                }
                break;
            }
            default:break;
        }
    }
}

About

Android计步模块(类似微信运动,支付宝计步,今日步数),记录当天从0点到23:59的步数


Languages

Language:Java 100.0%