如何在午夜重置步骤?

时间:2017-03-31 07:02:56

标签: android alarmmanager

我有一个在后台运行的服务,它使用设备传感器跟踪用户步骤。我的问题是我的应用程序没有在午夜将步骤重置为零。我有一个广播接收器,我已经把警报管理器,但我被困住如何更新我的步骤UI片段为零?

这是我的片段:

private void UpDateUi(int currentStep) {
    if (backgroundStepsDetails == null || backgroundStepsDetails.isEmpty()) {
        total_step = StepDetector.CURRENT_SETP;
        mDistanceInKm = getDistance();
        calories = countCalories();


    } else {
        if (total_step == 0) {
            total_step = Utilities.getIntValue(backgroundStepsDetails.get("total_steps"));

        }

        int temp = (mLastStepCount == StepDetector.CURRENT_SETP) ? 0 : StepDetector.CURRENT_SETP - mLastStepCount;
        mLastStepCount = StepDetector.CURRENT_SETP;
        Log.i(TAG, String.valueOf(temp));


        if (sessionDetails != null) {


            if (!SharedPreferenceManager.singleton().getString("LastFreeWalkSessionId").equals(sessionDetails.get("id"))) {
                total_step = total_step + temp + Utilities.getIntValue(sessionDetails.get("total_steps"));
                mDistanceInKm = getDistance() + Utilities.convertStringToDouble(backgroundStepsDetails.get("distance"))
                        + Utilities.convertMetersToKilometers(Utilities.convertStringToDouble(sessionDetails.get("total_distance_covered")));
                calories = countCalories() + Utilities.convertStringToDouble(backgroundStepsDetails.get("calories")) + Utilities.convertStringToDouble(sessionDetails.get("kalburnt"));
                SharedPreferenceManager.singleton().save("LastFreeWalkSessionId", sessionDetails.get("id"));
            } else {
                total_step = total_step + temp;
                mDistanceInKm = getDistance() + Utilities.convertStringToDouble(backgroundStepsDetails.get("distance"));
                calories = countCalories() + Utilities.convertStringToDouble(backgroundStepsDetails.get("calories"));
            }

        } else {
            total_step = total_step + temp;
        }


        if (mDistanceInKm == 0d) {
            mDistanceInKm = Utilities.convertStringToDouble(backgroundStepsDetails.get("distance"));
        }
            mDistanceInKm = getDistance() + Utilities.convertStringToDouble(backgroundStepsDetails.get("distance"));



        if (calories == 0d) {
            calories = Utilities.convertStringToDouble(backgroundStepsDetails.get("calories"));
            System.out.println("================CurrentCalories==========================");
            System.out.println(calories);

        }

            calories = countCalories() + Utilities.convertStringToDouble(backgroundStepsDetails.get("calories"));
            System.out.println("================UpdatedCalories==========================");
            System.out.println(calories);




    }
    textViewCalories.setText(Utilities.round(calories, 2) + getString(R.string.kcal));
    textViewDistance.setText(String.format("%.2f", mDistanceInKm) + getString(R.string.kmText));
    textViewStepCounter.setText(String.format("%,d", total_step));

    seekProgress.setProgress(total_step);


    boolean isRowPresent = dbController.isTodayDataPresent(currentDateandTime);


    if (isRowPresent) {

        dbController.updateSteps(String.valueOf(mDistanceInKm), String.valueOf(""), String.valueOf(total_step), String.valueOf((Utilities.round(calories, 2))));
    } else {
        storeStepsDataInDb();

    }

}

这是我的接收者:

public class ConnectionReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equalsIgnoreCase("com.journaldev.broadcastreceiver.SOME_ACTION")) {
        // Toast.makeText(context, "SOME_ACTION is received", Toast.LENGTH_LONG).show();

        Intent notificationIntent = new Intent(context, HomeActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.HOUR, 0);
        calendar.set(Calendar.AM_PM, Calendar.AM);

        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), 1000 * 60 *60 *24, contentIntent);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Mobiefit Walk")
                .setAutoCancel(false)
                .setOngoing(true)
                .setContentIntent(contentIntent);
        intent.hasExtra("TodayStep");
        final String action = intent.getAction();

       if(Intent.ACTION_TIME_TICK.equals(action) ||   Intent.ACTION_DATE_CHANGED.equals(action)){
           if(MobieFitWalkApp.mInstance != null){
              // MobieFitWalkApp.mInstance.setStepCount(0);
           }

       }
        Log.i("",String.valueOf(intent.getIntExtra("TodayStep", 0)));
        if (intent.getIntExtra("TodayStep", 0) > 0) {

            if (intent.getIntExtra("YourGoal", 0) != 0) {

                if (calculatePercentSixty(intent.getIntExtra("TodayStep", 0), intent.getIntExtra("YourGoal", 0))) {
                    mBuilder.setContentText("You’ve already completed" + intent.getIntExtra("TodayStep", 0) + " steps today!");
                } else if (calculatePercentEighty(intent.getIntExtra("TodayStep", 0), intent.getIntExtra("YourGoal", 0))) {
                    mBuilder.setContentText("You’re just" + +intent.getIntExtra("TodayStep", 0) + " steps away from today’s goal!");
                } else if (calculateGoal(intent.getIntExtra("TodayStep", 0), intent.getIntExtra("YourGoal", 0))) {
                    mBuilder.setContentText("Congrats! You’ve met your activity goal today!");
                } else {
                    mBuilder.setContentText("Today's Steps : " + " " + intent.getIntExtra("TodayStep", 0) + "");
                }
            }
        }

        else{
            mBuilder.setContentText("Today's Steps : " + " " + intent.getIntExtra("TodayStep", 0) + "");
        }
        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(1, mBuilder.build());
        Notification n;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            n = mBuilder.build();
        } else {
            n = mBuilder.getNotification();
        }

        n.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;

    }
}

有人能告诉我怎样才能实现这个目标?

1 个答案:

答案 0 :(得分:0)

1)将数据(步骤)保存在BroadCast Receiver的sqlite或sharedPreferences中。

2)在Activity / Fragment中用于显示步数的任何内容,从sqlite中读取步骤并在UI中显示。