First of all I am only getting started with android and java so please be indulgent.
So, i want to make a "jump" detector using the sensor in Android devices.
Basically i want to use the information returned by my sensor and compare it to detect if the user jumped or not.
Physics is pretty simple, I only need two very close value (a jump last for about a second) of the Y-axis and compare the average of these two values to a "limit value".
My main problem is I don't know how to get two differents values from my sensor.
I tried to use the System.currentTimeMillis() to catch a value at a very specific moment and compare it with a older or newer value, but it doesn't seem to work very well.
Here is my code.
public void onSensorChanged(SensorEvent event) {
acceleration.setText("X: " +event.values[0]+ " Y:" +event.values[1] + "Z: " +event.values[2] +"Nombre de sauts" +saut);
Yacc = event.values[1];
long curTime = System.currentTimeMillis();
long lastUpdate = curTime - 400;
if((curTime - lastUpdate)>390){
lastUpdate = curTime;
float moyY = (Yacc - last_Yacc)/2;
if(moyY > 5){
saut = saut +1;
}
compteur.setText("currenttime " +curTime + " lastUpdate "+ lastUpdate +" saut=" +saut + " Valeur moyenne" +moyY + " saut=" +saut);
}
last_Yacc = Yacc;
}
I thank you all in advance for you consideration.
Related
I have I time picker in my code and when I press the edit text it shows up and everything looks good but I want to set a default time for my time picker so when the user opens the time picker it will be shown a specific time like "3:34 am" as a default time for the user, how can I do that?. Any help will be appreciated. { you can use java and kotlin}
My time picker
private fun showTimePicker() {
picker = MaterialTimePicker.Builder()
.setTimeFormat(TimeFormat.CLOCK_12H)
.setHour(12)
.setMinute(0)
.setTitleText("حدد الموعد الذي تريدة")
.build()
picker.show(supportFragmentManager, "AdhanNotifacations")
picker.addOnPositiveButtonClickListener {
if (picker.hour > 12) {
fajrEditTxt.setHint(String.format("%02d", picker.hour - 12) + ":"
+ String.format("%02d", picker.minute) + " PM")
} else {
fajrEditTxt.setHint(String.format("%02d", picker.hour) + ":"
+ String.format("%02d", picker.minute) + " AM")
}
calender = Calendar.getInstance()
calender[Calendar.HOUR_OF_DAY] = picker.hour
calender[Calendar.MINUTE] = picker.minute
calender[Calendar.SECOND] = 0
calender[Calendar.MILLISECOND] = 0
}
}
You were almost there, just change the value of setHour and setMinute to the hour and minute you want respectively.
I made few changes to your code to show default time of 3:34am:
picker = MaterialTimePicker.Builder()
.setTimeFormat(TimeFormat.CLOCK_12H)
.setHour(3) //3 hour
.setMinute(34) //34 minutes, so 3:34am
.setTitleText("حدد الموعد الذي تريدة")
.build()
If you want to show hour in PM, just add 12 to the hour. For example:
setHour(15)// will show 3pm
Let me know if you have any questions. Thanks.
EDIT
If you want to show time in strict HH:MM format you can keep your code and go as-is. But if you do not wish to be strict, then use the following code:
For example, this will show 9:3PM instead of 09:03PM.
//removed unnecesarry String.format
if(picker.hour > 12){
fajrEditTxt.setHint((picker.hour - 12).toString() + ":" + (picker.minute).toString() + " PM")
}else{
fajrEditTxt.setHint((picker.hour).toString() + ":" + (picker.minute).toString() + " AM")
}
OP asked how to access time:
val time = if(picker.hour > 12){
String.format("%02d",picker.hour - 12) + ":" + String.format("%02d", picker.minute) + " PM"
}else{
String.format("%02d",picker.hour) + ":" + String.format("%02d", picker.minute) + " AM"
}
time //use time, its in HH:MM format
fajrEditTxt.setHint(time)
I'm not understanding what it is you're asking, but if you're trying to sort the times in the library, you could place the times in a list and then sort the list, or if you just want to compare the magnitude values of two different times with just a couple of lines of code, you could remove the time separators from the time strings, convert those values to numbers ( of your choice ) and compare. The numbers generated from the conversions have no real world values, but their magnitudes with respect to one another will always be valid.
Oh. Snippet editor does not support touch devices.
Ok then.
String time = "00:00";
time.replace( ":", "" );
Double d = Double.parseDouble( time );
So now you can do with d as you wish. As you can see you don't have to bother with whether any of the values are to great. If you're dealing with a pair of valid times it's their relative magnitudes that matters. Of course if the time is in 12 Hr format, you'll need to strip the time modifier ( A, a, P, p, Am, AM .... ) too.
I have created a proximity service which performs basic on/off of screen depending on the sensor value. I needed to time these sensor changes so i used the following code
override fun onSensorChanged(event: SensorEvent) {
val distance = event.values[0]
val max = event.sensor.maximumRange
val startTime = System.currentTimeMillis()
if (distance < Math.min(max, 8.toFloat())) {
listener.onNear()
} else {
listener.onFar()
}
val endTime = System.currentTimeMillis()
System.out.println("That took " + (endTime - startTime) + " milliseconds")
}
I am getting the values in log cat but i need to print these on my app, my app activity is in another java class called settings activity. I have added a textview called "Time taken" how to push those values of (endTime-startTime) to this textview??? or should i have to use any other layout comonent??
My settings layout image
You need to set id of that TextView and then bindview in java and after it you can set text into TextView
Your XML should have this widget
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
now in your java, bind this textview with id like
TextView tv=(TextView)findViewById(R.id.textView1);
now you have tv object of TextView that will help you to setText() like
tv.setText("That took " + (endTime - startTime) + " milliseconds");
make the variables you want to access public static
and then access them by
tv.setText(YourActivity.YourVariable);
When I get the contents of the system clipboard, there is a small chance that the call will hang for exactly 10 seconds.
I ran into this because my popup menu creation would occasionally hang because I was checking the state of the clipboard to enable / disable a paste option.
After narrowing it down to getSystemClipboard().getContents(), I wrote a console app that just loops and queries it. Under windows it runs fine, but under Linux and X, I get quite a few hangs.
In my GUI app, the 10 second hang will be cut short if there are any UI events (ie. mouse movement).
I've also ran my console app pointing to a Xvfb (headless X virtual frame buffer) and if there is only one console app connected to it hitting the clipboard it usually succeeds, but having a few copies of the app hitting the same X display the errors start happening.
Below is an example app. I was wondering if this showed the same problem for you.
public class main {
public static void main(String[] args)
{
System.out.println("Clipboard class: " + Toolkit.getDefaultToolkit().getSystemClipboard().getClass() );
long progstart_ts = System.currentTimeMillis();
int slowcount = 0;
int MAXLOOP = 1000;
for(int i = 0; i < MAXLOOP; i++)
{
long start_ts = System.currentTimeMillis();
Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
long end_ts = System.currentTimeMillis();
long elapsed = end_ts - start_ts;
if ( elapsed > 1000 )
{
System.out.println("Loop " + i + ", " + elapsed);
slowcount++;
}
}
long progend_ts = System.currentTimeMillis();
long progelapsed = progend_ts - progstart_ts;
System.out.println("Total time: " + progelapsed);
System.out.println("Good/bad: " + (MAXLOOP - slowcount) + "/" + slowcount);
}
}
I am trying to get the total time spent by the map phase and by the reduce phase in a job run. So I've tried to use the JobCounters MILLIS_MAPS and MILLI_REDUCES, but its seems that when I try to get the value these one is always zero. Besides this, I also notice that these counters aren't printed when the job is completed successfully, which leads to my questions.
The JobCounters are in use in Hadoop 2.6.0? Were they deprecated?
If not, how can we use them correctly?
This is what I've tried so far:
Date startTime = new Date();
System.out.println("Job started: " + startTime);
job.waitForCompletion(true);
Date end_time = new Date();
System.out.println("Job ended: " + end_time);
System.out.println("The job took " +
(end_time.getTime() - startTime.getTime()) /1000 + " seconds.");
Counters counters = job.getCounters();
Counter mapTime = counters.findCounter(JobCounter.MILLIS_MAPS);
System.out.println(mapTime.getDisplayName() + " : " + mapTime.getValue());
Counter reduceTime = counters.findCounter(JobCounter.MILLIS_REDUCES);
System.out.println(reduceTime.getDisplayName() + " : " +reduceTime.getValue());
UPDATE:
It seems that I can only print these counters when running yarn. Any ideas how to get them without running yarn?
Thanks for the attention!
I'm currently referring to this link to create the timer for my program.
But I don't know how to get the time when timer is stopped.
I think System.getcurrentTimeMillis() cannot be used at this moment.
So, how can I get the current time when timer is stopped with the method mentioned?
EDIT :
I'm currently building a simple 4X4 memory game, and I have timer in my program, using the method from this link.
At the end of the game, I'll use a showMessageDialog to display the number of clicks and time used. However, I'm just able to display the clicks, which is the easiest part.
Based on the method from the link, I think I can use an easy way such as
JOptionPane.showMessageDialog( null, "Congratulations. \n You have win the game with " + click + " click(s) in " + hour + "hour(s) " + minute + " minute(s) " + second + " second(s).", "Win", 1);
Is there any better way of doing it?
You can use
Date d = new Date();
DateFormat df = new SimpleDateFormat("hh:mm:ss");
String dateStr = df.format(d);