Skip to content

Commit

Permalink
优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
linglongxin24 committed May 8, 2017
1 parent 64dbc96 commit c84ceed
Show file tree
Hide file tree
Showing 10 changed files with 297 additions and 53 deletions.
28 changes: 25 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
<!--计歩-->
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />

<uses-feature android:name="android.hardware.sensor.accelerometer" />

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />

<uses-feature
android:name="android.hardware.sensor.stepcounter"
android:required="true" />
Expand All @@ -16,11 +19,11 @@
android:required="true" />

<application
android:name=".app.MyApplication"
android:allowBackup="true"
android:icon="@mipmap/logo"
android:label="@string/app_name"
android:supportsRtl="true"
android:name=".app.MyApplication"
android:theme="@style/AppTheme">
<activity android:name=".activity.MainActivity">
<intent-filter>
Expand All @@ -29,8 +32,12 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activity.SetPlanActivity" android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>
<activity android:name=".activity.HistoryActivity" android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>
<activity
android:name=".activity.SetPlanActivity"
android:theme="@style/Theme.AppCompat.Light.NoActionBar" />
<activity
android:name=".activity.HistoryActivity"
android:theme="@style/Theme.AppCompat.Light.NoActionBar" />

<service
android:name=".step.service.StepService"
Expand All @@ -47,6 +54,21 @@
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
</intent-filter>
</service>
<service
android:name=".pedometer.StepsDetectService"
android:priority="1000"
android:process="cn.bluemobi.dylan.step..pedometer.StepsDetectService">
<intent-filter>
<!-- 系统启动完成后会调用-->
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.DATE_CHANGED" />
<action android:name="android.intent.action.MEDIA_MOUNTED" />
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="android.intent.action.ACTION_TIME_TICK" />
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
</intent-filter>
</service>

</application>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import cn.bluemobi.dylan.step.R;
import cn.bluemobi.dylan.step.adapter.CommonAdapter;
import cn.bluemobi.dylan.step.adapter.CommonViewHolder;
import cn.bluemobi.dylan.step.step.pojo.StepData;
import cn.bluemobi.dylan.step.step.bean.StepData;
import cn.bluemobi.dylan.step.step.utils.DbUtils;

/**
Expand Down
58 changes: 42 additions & 16 deletions app/src/main/java/cn/bluemobi/dylan/step/activity/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,27 @@
import android.view.View;
import android.widget.TextView;

import com.orhanobut.logger.Logger;

import org.json.JSONException;
import org.json.JSONObject;

import cn.bluemobi.dylan.step.R;
import cn.bluemobi.dylan.step.pedometer.StepsDetectService;
import cn.bluemobi.dylan.step.step.config.Constant;
import cn.bluemobi.dylan.step.step.service.StepService;
import cn.bluemobi.dylan.step.step.utils.SharedPreferencesUtils;
import cn.bluemobi.dylan.step.step.utils.StepCountModeDispatcher;
import cn.bluemobi.dylan.step.view.StepArcView;

/**
* 记步主页
*/
public class MainActivity extends AppCompatActivity implements Handler.Callback, View.OnClickListener {
private TextView tv_data;
private StepArcView cc;
private TextView tv_set;
private TextView tv_isSupport;
private Handler delayHandler;
private SharedPreferencesUtils sp;

private void assignViews() {
Expand All @@ -45,26 +53,43 @@ protected void onCreate(Bundle savedInstanceState) {
addListener();
}


private void addListener() {
tv_set.setOnClickListener(this);
tv_data.setOnClickListener(this);
}

private void initData() {
sp = new SharedPreferencesUtils(this);
//获取用户设置的计划锻炼步数,没有设置过的话默认7000
String planWalk_QTY = (String) sp.getParam("planWalk_QTY", "7000");
//设置当前步数为0
cc.setCurrentCount(Integer.parseInt(planWalk_QTY), 0);
//判断设置是否支持计步
if (StepCountModeDispatcher.isSupportStepCountSensor(this)) {
tv_isSupport.setText("计步中...");
delayHandler = new Handler(this);
new Handler(this);
setupService();
} else {
tv_isSupport.setText("该设备不支持计步");
startService(new Intent(this, StepsDetectService.class));
StepsDetectService.setOnStepDetectListener(new StepsDetectService.OnStepDetectListener() {
@Override
public void onStepDetect(int steps) {
JSONObject ret = new JSONObject();
try {
ret.put("steps", steps);
tv_isSupport.setText(steps);
Logger.d("steps=" + steps);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
// tv_isSupport.setText("该设备不支持计步");
}
}


private void addListener() {
tv_set.setOnClickListener(this);
tv_data.setOnClickListener(this);
}


private boolean isBind = false;
private Messenger mGetReplyMessenger = new Messenger(new Handler(this));
private Messenger messenger;
Expand Down Expand Up @@ -132,13 +157,6 @@ public void onServiceDisconnected(ComponentName name) {
}
};

@Override
public void onDestroy() {
super.onDestroy();
if (isBind) {
this.unbindService(conn);
}
}

@Override
public void onClick(View v) {
Expand All @@ -151,4 +169,12 @@ public void onClick(View v) {
break;
}
}

@Override
public void onDestroy() {
super.onDestroy();
if (isBind) {
this.unbindService(conn);
}
}
}
112 changes: 112 additions & 0 deletions app/src/main/java/cn/bluemobi/dylan/step/pedometer/StepDetector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package cn.bluemobi.dylan.step.pedometer;

/*
* Pedometer - Android App
* Copyright (C) 2009 Levente Bagi
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

/**
* Detects steps and notifies all listeners (that implement StepListener).
* @author Levente Bagi
* @todo REFACTOR: SensorListener is deprecated
*/
public class StepDetector implements SensorEventListener {

private float mLimit = 10;
private float mLastValues[] = new float[3*2];
private float mScale[] = new float[2];
private float mYOffset;

private float mLastDirections[] = new float[3*2];
private float mLastExtremes[][] = { new float[3*2], new float[3*2] };
private float mLastDiff[] = new float[3*2];
private int mLastMatch = -1;

private StepListener mListener;

public StepDetector() {
int h = 480;
mYOffset = h * 0.5f;
mScale[0] = - (h * 0.5f * (1.0f / (SensorManager.STANDARD_GRAVITY * 2)));
mScale[1] = - (h * 0.5f * (1.0f / (SensorManager.MAGNETIC_FIELD_EARTH_MAX)));
}

public void setSensitivity(float sensitivity) {
mLimit = sensitivity;
}

public void setStepListener(StepListener sl) {
mListener = sl;
}

@SuppressWarnings("deprecation")
public void onSensorChanged(SensorEvent event) {
Sensor sensor = event.sensor;
synchronized (this) {
if (sensor.getType() == Sensor.TYPE_ORIENTATION) {
}
else {
int j = (sensor.getType() == Sensor.TYPE_ACCELEROMETER) ? 1 : 0;
if (j == 1) {
float vSum = 0;
for (int i=0 ; i<3 ; i++) {
final float v = mYOffset + event.values[i] * mScale[j];
vSum += v;
}
int k = 0;
float v = vSum / 3;

float direction = (v > mLastValues[k] ? 1 : (v < mLastValues[k] ? -1 : 0));
if (direction == - mLastDirections[k]) {

int extType = (direction > 0 ? 0 : 1);
mLastExtremes[extType][k] = mLastValues[k];
float diff = Math.abs(mLastExtremes[extType][k] - mLastExtremes[1 - extType][k]);

if (diff > mLimit) {

boolean isAlmostAsLargeAsPrevious = diff > (mLastDiff[k]*2/3);
boolean isPreviousLargeEnough = mLastDiff[k] > (diff/3);
boolean isNotContra = (mLastMatch != 1 - extType);

if (isAlmostAsLargeAsPrevious && isPreviousLargeEnough && isNotContra) {
if(mListener != null){
mListener.onStep();
}
mLastMatch = extType;
} else {
mLastMatch = -1;
}
}
mLastDiff[k] = diff;
}
mLastDirections[k] = direction;
mLastValues[k] = v;
}
}
}
}

public void onAccuracyChanged(Sensor sensor, int accuracy) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package cn.bluemobi.dylan.step.pedometer;
public interface StepListener {
public void onStep();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package cn.bluemobi.dylan.step.pedometer;

import android.app.Service;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.IBinder;

public class StepsDetectService extends Service {

private SensorManager mSensorManager;
private Sensor mSensor;
private StepDetector mStepDetector;

public static int steps = 0;

@Override
public void onCreate() {

mStepDetector = new StepDetector();
registerDetector();
mStepDetector.setStepListener(new StepListener() {
@Override
public void onStep(){
steps ++;
if(mOnStepDetectListener != null){
mOnStepDetectListener.onStepDetect(steps);
}
}
});
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
steps = 0;
return super.onStartCommand(intent, flags, startId);
}

public void registerDetector() {
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorManager.registerListener(mStepDetector, mSensor, SensorManager.SENSOR_DELAY_FASTEST);
}

@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onDestroy() {
mOnStepDetectListener = null;
unRegisterDector();
steps = 0;
}

public void unRegisterDector(){
if(mStepDetector != null && mSensorManager != null){
mStepDetector.setStepListener(null);
mSensorManager.unregisterListener(mStepDetector);
}
}

public interface OnStepDetectListener {
public void onStepDetect(int steps);
}

public static OnStepDetectListener mOnStepDetectListener = null;

public static void setOnStepDetectListener(OnStepDetectListener mListener){
mOnStepDetectListener = mListener;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.bluemobi.dylan.step.step.pojo;
package cn.bluemobi.dylan.step.step.bean;

import com.litesuits.orm.db.annotation.Column;
import com.litesuits.orm.db.annotation.PrimaryKey;
Expand Down
Loading

0 comments on commit c84ceed

Please sign in to comment.