Android传感器计步器

暑假里做了一个安卓计步器,不依靠GPS,也不用联网,只依靠重力加速度传感器和三轴磁力传感器,就可以实现记录步数和记录行走轨迹。我还加入了摇一摇切歌的功能,享受运动的乐趣~~

发布的apk可以在这里下载:pan.baidu.com/s/1i3gw3sh (打算以后完备了再发布到应用市场,所以欢迎大家吐槽评论)

这个项目只用了三天时间(作为自己的第一个安卓作品,我觉得还是很快的~~),当时完全是被deadline给逼出来的。老师对这个项目赞不绝口,觉得我一个正在上预科的孩子,学校里C语言还没有开课,就能自己做得这么好,于是果断给99分,哈哈~~不过,我觉得这个项目有很多地方可以改进和完善,因此在这里把这个项目总结一下,以备查询,也欢迎大家给出意见。以后有时间,再推出2.0、3.0等版本咯咯~~

计步器的原理是基本的物理知识,不再赘述,以后有时间可以单独写一篇,说明我是如何推导的。下面是所有代码:

第一部分:manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.pedometre"
    android:versionCode="1"
    android:versionName="1.0" >
    
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name">
        <activity
            android:theme="@style/myTheme"
            android:name=".MainActivity"
            android:label="@string/app_name" >
        </activity>
        <activity 
            android:theme="@android:style/Theme.NoTitleBar"
            android:name="com.example.pedometre.splash">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.example.pedometre.aide"
                  android:theme="@style/myTheme">
             <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity" />
        </activity>
        <activity android:name="com.example.pedometre.carteDraw"
                  android:theme="@style/myTheme">
             <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity" />
        </activity>
        <service android:name="com.example.pedometre.StepCounterService"></service>
        <activity android:name="com.example.pedometre.mPlayer"
                  android:theme="@style/myTheme">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity" />
        </activity>
        <service android:name="com.example.pedometre.mPlayerService"></service>
    </application>

</manifest>
第二部分:mainActivity

package com.example.pedometre;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.DecimalFormat;
import java.util.ArrayList;

import com.example.pedometre.StepDetector;
import com.example.pedometre.StepCounterService;

import android.support.v4.widget.DrawerLayout;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {
	private DrawerLayout mDrawerLayout = null;
	private ListView mylistview;
	private ArrayList<String> list = new ArrayList<String>();
	private TextView tv_step;
	private TextView sy_dis;
	private TextView sy_cal;
	private TextView sy_vit;
	private Button btn_start;
	private Button btn_stop;
	private Thread mThread;
	private Chronometer timer;
	public static int total_step = 0;
	private int total_step1=0;
	private float pep_weight=60;
	private float per_step=0.5f;
	private float last_time=0;
	private float kaluli=0;
	private float x=0;
	private float st_x=0;
	private float st_y=0;
	private String inputName="";
	private SensorManager sm1;
	private SensorManager sm2;
	

	float[] accelerometerValues = new float[3];
	float[] magneticFieldValues = new float[3];
	
	public static float[]da_st=new float[10000];
	public static float max_width=0f,max_height=0f;

	
    @SuppressLint("HandlerLeak") Handler mHandler = new Handler(){
		@Override
		public void handleMessage(android.os.Message msg) {
			super.handleMessage(msg);
			if(msg.what == 0x00){
				countStep();
				tv_step.setText(total_step+"");
				last_time=last_time/1000/3600;
				kaluli=pep_weight*last_time*30/(last_time/per_step/total_step*400*60);
				DecimalFormat decimalFormat=new DecimalFormat("0.00");
				sy_cal.setText(decimalFormat.format(kaluli)+"kcal");
				
				 float[] values = new float[3];
			     float[] R = new float[9];
			     SensorManager.getRotationMatrix(R, null, accelerometerValues, magneticFieldValues);	      
			     SensorManager.getOrientation(R, values);
			     da_st[total_step]=values[0];
			     
			   /* st_x=(float) (st_x+Math.cos(values[0]));
					st_y=(float) (st_y+Math.sin(values[0]));
					if (Math.abs(st_x)>max_height){
						max_height=Math.abs(st_x);
					}
					if (Math.abs(st_y)>max_width){
						max_width=Math.abs(st_y);
					}
			   */
				sy_dis.setText(decimalFormat.format(total_step*per_step/1000)+"km");
				
				sy_vit.setText(decimalFormat.format(total_step*per_step/1000/last_time)+"km/h");
			}
		}
	};
	
	private void countStep() {
		if(StepDetector.CURRENT_SETP % 2 == 0){
			total_step = StepDetector.CURRENT_SETP;
		}else{
			total_step = StepDetector.CURRENT_SETP + 1;
		}
		Log.i("total_step", StepDetector.CURRENT_SETP+"");
		total_step = StepDetector.CURRENT_SETP;
	};
	
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        sm1 = (SensorManager) getSystemService(SENSOR_SERVICE);
        sm2 = (SensorManager) getSystemService(SENSOR_SERVICE);
      
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        forceShowOverflowMenu();
        tv_step = (TextView) findViewById(R.id.tv_step);
        sy_dis = (TextView) findViewById(R.id.sy_distance);
        sy_cal = (TextView) findViewById(R.id.sy_caluli);
        sy_vit = (TextView) findViewById(R.id.sy_vitesse);
		btn_start = (Button) findViewById(R.id.btn_start);
		btn_stop = (Button) findViewById(R.id.btn_stop);
		timer = (Chronometer) findViewById(R.id.chronometer);
		mylistview = (ListView)findViewById(R.id.left_drawer);
		list.add("Poids(kg):                                    (60kg par défaut)");
		list.add("Longueur de chaque étape(cm):                 (50cm par défaut)");
		list.add("La sensibilité du capteur:                    (3 par défaut)");
		list.add("Intervalle temporel entre deux étapes(ms):    (500 par défaut)");
		ArrayAdapter<String> myArrayAdapter = new ArrayAdapter<String>
		(this,android.R.layout.simple_list_item_1,list);
		mylistview.setAdapter(myArrayAdapter);
		mylistview.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
				if(list.get(arg2).contains("Poids")){
					inputTitleDialog();
					int t=0;
					try{
						t=Integer.parseInt(inputName);
					}catch(Exception e){}
					if (t>0){
					    list.set(arg2,"Poids(kg):                                    ("+inputName+"kg par défaut)");
					    pep_weight=t;
					}
				}
				if(list.get(arg2).contains("Longueur")){
					inputTitleDialog();
					int t=0;
					try{
						t=Integer.parseInt(inputName);
					}catch(Exception e){}
					if (t>0){
					    list.set(arg2,"Longueur de chaque étape(cm):                 ("+inputName+"cm par défaut)");
					    per_step=(float) (t*1.0/100);
					}
				}
				if(list.get(arg2).contains("sensibilité")){
					inputTitleDialog();
					int t=0;
					try{
						t=Integer.parseInt(inputName);
					}catch(Exception e){}
					if (t>0){
					    list.set(arg2,"La sensibilité du capteur:                    ("+inputName+" par défaut)");
					    StepDetector.SENSITIVITY=t;
					}
				}
				if(list.get(arg2).contains("Intervalle")){
					inputTitleDialog();
					int t=0;
					try{
						t=Integer.parseInt(inputName);
					}catch(Exception e){}
					if (t>0){
					    list.set(arg2,"Intervalle temporel entre deux étapes(ms):    ("+inputName+" par défaut)");
					    StepDetector.time_jg=t;
					}
				}
			}
		});
    }
阅读终点,创作起航,您可以撰写心得或摘录文章要点写篇博文。去创作
  • 7
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

薄帷清风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值