每隔午夜更新片段UI

时间:2017-04-06 04:45:18

标签: android android-fragments broadcastreceiver

我正在开发一个计步器,我必须在每个午夜将步骤重置为0。我的UI在片段中。我很困惑使用报警管理器或日期时间监听器。我的接收器是在一个单独的类。目前正在显示通知中的步骤

这是我的接收器代码:

public class ConnectionReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equalsIgnoreCase("com.journaldev.broadcastreceiver.SOME_ACTION")) {

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

        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;

    }
}

我的步骤跟踪逻辑是处于片段中的处理程序。

 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();

    }
}

有谁能告诉我如何在每个午夜重置我的步骤?

0 个答案:

没有答案