看一下效果图:
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; //循环取当前时刻的步数中间的间隔时间 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 + "步"); } }
布局文件:
<?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源码点击打开链接