如何每天制作应用记录步骤数据?

时间:2017-05-13 23:57:38

标签: android android-fragments android-sqlite android-service

我试图为我的应用制作一个时间轴片段,显示每天所采取的步数,我在片段中显示步数,

  1. 我每24小时后重置一次这个值很麻烦。
  2. 我已将计步变量声明为静态变量,当app关闭时会重置,但我已经声明了一个服务类,这是我的步数逻辑发生的地方。
  3. 每天的步骤都会插入到sqlite数据库中,并显示在listview中的另一个片段中。
  4. public class HomeFragment extends Fragment {

    TextView tv_steps;
    View myView;
    
    private BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            int stepCountValue = intent.getExtras().getInt("StepCount");
    
            tv_steps.setText(String.valueOf(stepCountValue));
    
            // Toast.makeText(context, stepCountValue + "", Toast.LENGTH_SHORT).show();
        }
    };
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        myView = inflater.inflate(R.layout.home_layout, container, false);
    
    
        RelativeLayout rl = (RelativeLayout) inflater.inflate(R.layout.home_layout, container, false);
        tv_steps = (TextView) rl.findViewById(R.id.tv_steps);
    
        return rl;
    }
    
    @Override
    public void onResume() {
        super.onResume();
        getActivity().registerReceiver(receiver, new IntentFilter("stepcountvalue"));
    
    }
    
    @Override
    public void onPause() {
        super.onPause();
        //getActivity().unregisterReceiver(receiver);
    }
    

    }

    我的步数逻辑服务类:

    public class StepCounterService extends Service implements SensorEventListener {
    
    SensorManager sensorManager;
    
    static int initialStepCount = 0;
    public static int stepCount = 0;
    
    SQLiteDatabase database;
    private int numberOfStepsToBeInserted;
    
    
    @Override
    public void onCreate() {
        super.onCreate();
    
        sensorManager = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE);
        database = openOrCreateDatabase("Steps", MODE_PRIVATE, null);
    
        try {
            database.execSQL("CREATE TABLE IF NOT EXISTS steps (date TEXT, steps_count INT(10))");
        } catch (Exception e) {
            e.printStackTrace();
        }
    
        final Handler handler = new Handler();
        Runnable run = new Runnable() {
            @Override
            public void run() {
    
                //numberOfStepsToBeInserted = stepCount;
    
                //stepCount = stepCount - numberOfStepsToBeInserted;
                //stepCount = 0;
    
                String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
    
                database.execSQL("INSERT INTO steps(date, steps_count) VALUES('" + date + "', '" + stepCount + "')");
    
    
                Cursor c = database.rawQuery("SELECT * FROM steps", null);
    
                int dateIndex = c.getColumnIndex("date");
                int stepIndex = c.getColumnIndex("steps_count");
    
                c.moveToFirst();
    
                if (c.getCount() >= 1) {
                    while (c.moveToNext()) {
                        Log.d("----------------->", c.getString(dateIndex) + "  " + c.getInt(stepIndex));
                    }
    
                } else {
                    Log.i("Check empty", "Cursor empty");
                }
    
                handler.postDelayed(this, 86400000);
    
            }
        };
        handler.post(run);
    
    
    }
    
    @Override
    public void onSensorChanged(SensorEvent event) {
        if (initialStepCount == 0) {
            initialStepCount = (int) event.values[0];
        }
    
        stepCount = (int) event.values[0] - initialStepCount;
    
        Intent sendStepCount = new Intent("stepcountvalue");
        sendStepCount.putExtra("StepCount", stepCount - numberOfStepsToBeInserted);
        sendBroadcast(sendStepCount);
    }
    
    
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    
    }
    
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Sensor countSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
        if (countSensor != null) {
            sensorManager.registerListener(this, countSensor, SensorManager.SENSOR_DELAY_UI);
        } else {
        }
        return START_STICKY;
    }
    
    
    @Override
    public void onDestroy() {
    }
    
    }
    

1 个答案:

答案 0 :(得分:0)

使用Google Fit API可能更容易实现此目的。你必须仔细研究一下才能弄清楚你需要使用哪一部分,但它可以提供更多的功能,并且比一直运行后台服务的性能要好得多。此外,在Android 6.0+中添加Doze and App Standby后,您的服务可能会在屏幕关闭时停止,从而使得结果数据不准确且不可靠。

如果您不想使用Google Fit API,我会建议您解决两个问题:

关于第三个问题(并不是真的看起来像一个问题,但是我假设它是一个,因为它之前列出了其他两个问题),这个问题有点过于笼统回答没有详细说明:

  

每天的步骤都将插入到sqlite数据库中,并显示在listview中的另一个片段中。

我建议找一些类型的教程来帮助你,而不是在Stack Overflow上询问它,例如this one