By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,190 Members | 1,503 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,190 IT Pros & Developers. It's quick & easy.

To build an step counter app using android studio

P: 7
can anybody shown me how to build an step counter app using android studio that will count the number of footstep and plot the number of footstep in graphical form for every 15 minutes?
Oct 21 '17 #1
Share this Question
Share on Google+
5 Replies

Expert 100+
P: 785
We are here to help you if you are stuck, not to do the coding for free. So show us what you have so far (list your code here), tell us the problem in this code, and we will help you.

To give you general advice how to learn developing such a program, we need more infos from you (your klnowledge level in java programming, especially the graphics, and android knowledge)
Oct 23 '17 #2

P: 7
public void step(long timeNs) {
numSteps= numSteps+1;//to add number steps by 1 when sensor detect one step have been made
TvSteps.setText(TEXT_NUM_STEPS + numSteps);//to display the newest number step

Handler handler=new Handler();
handler.postDelayed(new Runnable() {
public void run() {
},16*60000);//to update latest number of footstep 1 minutes after display step added

numStepsAdd=numSteps-numStepsA;//to calculate footstep added

Handler handleradd=new Handler();
handleradd.postDelayed(new Runnable() {
public void run() {
StepAdd.setText( TEXT_ADD_STEPS+numStepsAdd);
},15*60000);//to display step added every 15 minutes

Here is part of code, i wish to calculate number of step added for every 15 minutes no matter the sensor detect motion or not, and save it in 'numStepsAdd' and diaplay it. Then, update the latest number of steps into 'numStepsA' 1 minutes after displaying the 'numStepsAdd', so that i can use the formulae
'numStepsAdd=numSteps-numStepsA' to calculate numStepsAdd every 15 minutes. Then, repeat whole process for every 15 minutes.

But after i build my app, it will only show step added after every 15 minutes if the sensor detect steps, if the sensor does not detect any steps, the app will not show any changes in the 'numStepsAdd' until sensor detect steps is made.
But what i want is to show the number of steps added every 15 minutes no matter sensor detect steps or not. For example if there are 5 steps added, 'numStepsAdd' show 5, when there are no steps added, 'numStepsAdd' show 0, for every 15 minutes.

So, can sir tell me what is wrong in my code, and help me to do some correction on my code.
Nov 16 '17 #3

Expert 100+
P: 785
Please use coding tags around your code (the CODE-button, right next to the picture icon in the editor), so it is easier for me to refer to it by line numbers.

The calculation of "numStepsAdd" is inside the step() method an will only be calculated once in the beginning. The method will not be called again, so numStepsAdd will never change. Your handleradd will be called every 15 minutes, but will always print the same. to avoid that, you must shift the calculation of numStepsAdd into the run-method of handleradd. That means simply copy the line "numStepsAdd=numSteps-numStepsA;" and paste it immediately right before the line with the setText-command in handleradd.

On other thing: "handler" is executed every 16 minutes (although comment says every minute) and resets numStepA. Why not call it every 15 minutes, that means just put it inside "handleradd" also?

Based on your description I just guess that step() is called more than one time. Maybe every step you do? Then you have big problems, because then it will create 2 new handlers every time additionally. So if step() is called 3 times within 3 seconds, 6 handlers are running and will do their thing after around 15 minutes, which leads to chaos.
How to fix:
You must create both handlers outiside the step() method. That means in the method that calls step().
A handler then must do its thing and then at the end postDelayed() itself inside its own run() method.
That will fix your problem. Now the calculation will be called independently of step(), that means also if step will not be called anymore within 15 minutes
Nov 18 '17 #4

P: 7
Sir ,i have modify the code, but this time i use 1 minutes for testing, but i does not work too.
It does not show any value instead of 0 every time when there are some step added.
Attached Images
File Type: jpg code.jpg (46.5 KB, 73 views)
Nov 20 '17 #5

Expert 100+
P: 785
That code is better, (especially the placement of the numStepAdd computation) but not quite what I meant regarding the handlers.
You must make sure that the handlers are only initialized once.
And you don't need the second handler, you can do it all in one.
And after a handler was run, it must restart itself. You can do it in the main() method or for example in the constructor of a class.

You have your step-method inside a class, let's assume it's called MyClass.
Expand|Select|Wrap|Line Numbers
  1. public class MyClass
  2. {
  3.   public MyClass() {
  4.     ... 
  5.     initHandlers();
  6.   }
  7.   public void step(...) {
  8.     ...
  9.   }
  10.   private initHandlers() {
  11.      // first handler
  12.      Handler handler=new Handler();
  13.      Runnable action = new Runnable() {
  14.         @Override
  15.         public void run() {
  16.             numStepsAdd=numSteps-numStepsA;//to calculate footstep added
  17.             StepAdd.setText( TEXT_ADD_STEPS+numStepsAdd);
  18.             numStepsA=numSteps;
  19.             handler.postDelayed(this, 16*60000) // restart
  20.         }
  21.      };
  22.      handler.postDelayed(action, 16*60000) // first time start    
  23.   }
  24. }
Nov 21 '17 #6

Post your reply

Sign in to post your reply or Sign up for a free account.