日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android 计步器 计算当前的行走步数

發(fā)布時間:2024/3/13 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 计步器 计算当前的行走步数 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

?看一下效果圖:



MainActivity.java

package com.leng.jibuqi; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; import android.os.Message; import android.os.RemoteException; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.widget.TextView; import com.leng.jbq.ISportStepInterface; import com.leng.jbq.TodayStepManager; import com.leng.jbq.TodayStepService; public class MainActivity extends AppCompatActivity {private static String TAG = "MainActivity"; private static final int REFRESH_STEP_WHAT = 0; //循環(huán)取當前時刻的步數(shù)中間的間隔時間 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毫秒獲取一次計步數(shù)據(jù)刷新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 + "步"); } } 布局文件: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.leng.jibuqi.MainActivity"> <TextView android:id="@+id/stepTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="80dp" android:text="Hello World!" android:textSize="20sp" /> </RelativeLayout>
在app的build.gradle下引用jbq-debug.aar的aar包 完整代碼: apply plugin: 'com.android.application' android {compileSdkVersion 26 buildToolsVersion "26.0.2" defaultConfig {applicationId "com.leng.jibuqi" minSdkVersion 15 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" }buildTypes {release {minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' }} } repositories {flatDir {dirs 'libs' } } dependencies {compile fileTree(dir: 'libs', include: ['*.jar'])androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {exclude group: 'com.android.support', module: 'support-annotations' })compile(name:'jbq-debug', ext:'aar')compile 'com.android.support:appcompat-v7:26.+' compile 'com.android.support.constraint:constraint-layout:1.0.2' testCompile 'junit:junit:4.12' }


項目地址:點擊打開鏈接

jbq.aar源碼點擊打開鏈接

總結(jié)

以上是生活随笔為你收集整理的android 计步器 计算当前的行走步数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。