I am trying to record some audio from microphone on android wear, 3 seconds long.
In this case we can't do that with Thread.sleep(3000); because during those seconds main thread works.
Also I was trying with manipulate with time and I got current time in seconds from Calendar example:
Calendar c = Calendar.getInstance();
int seconds = c.get(Calendar.SECOND);
seconds = seconds + 2;
if (seconds == 60 || seconds == 61)
{
seconds = seconds - 60;
}
int sada; //with initialization here is pretty much same
do
{
sada = 105; //without this line is pretty much same
sada = c.get(Calendar.SECOND);
Log.d("SEKUNDI", String.valueOf(sada));
}while(sada != seconds);
Anyone help?
I started recording in main thread, and I created new thread in background and sleep it for 3 seconds. In a postBack method I called the stopRecording() method. Important code is:
Thread.currentThread();
Thread.sleep(3000);
This can be applied to any other similar problem. Thanks for comments.
Related
I have a simple code, I record the time when the user hits a button and subtract that time from the current time. It should give me the difference.
The funny part is that if I print my vars they are correct but when I make the subtraction the result is the year 1969 the time the system takes as reference.
This is my code:
// I register this time to later compare to the current time.
startTime = System.currentTimeMillis().toDouble()
timer = Timer()
timer.schedule(timerTask {
runOnUiThread {
advanceTimer()
}
}, 0, 60)
fun advanceTimer() {
//Total time since timer started, in seconds
val currentTime = System.currentTimeMillis().toDouble()
time = currentTime-startTime
}
This is what the console prints:
D/startTime: 2020:12:20:21:27:39:78
D/currentTime: 2020:12:20:21:29:49:21
D/time: 1969:12:31:18:02:09:42
Can somebody help me, please?
String time1 = "16:00:00";
String time2 = "19:00:00";
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
Date date1 = format.parse(time1);
Date date2 = format.parse(time2);
long difference = date2.getTime() - date1.getTime();
This is how you calculate the time difference in Java.
The substraction of your date calculation is difference between two date
if you set to SimpleDateFormat date just show the difference of,
you can see at this answer https://stackoverflow.com/a/21285226/5523669
This answer lead me the right way, actually my approach was good I just needed to make a function that showed only the difference.
I made this one. Hope it helps somebody.
Just call the function with the result of you operation, at the end I used Date().getTime() instead of System.currentTimeMillis() but I think it should be the same.
The complete code:
startTime = System.currentTimeMillis().toDouble()
timer = Timer()
timer.schedule(timerTask {
runOnUiThread {
advanceTimer()
}
}, 0, 60)
fun advanceTimer() {
//Total time since timer started, in seconds
val currentTime = System.currentTimeMillis().toDouble()
time = currentTime-startTime
// Shows the time in a label on the screen
timerString.text = differenceResult(time)
}
fun differenceResult(time: Long): String {
var x: Long = time / 1000
var seconds = x % 60
x /= 60
var minutes = x % 60
x /= 60
var hours = (x % 24).toInt()
x /= 24
var days = x
return String.format("%02d:%02d:%02d", hours, minutes, seconds)
}
Hey there i have a countdown timer which i used to show the user how much time is left in my app. But now i want if timer is running for 1 min and user closes the app after 30 sec and in next 5 sec user again open the app then time should start to run from remaining 25 seconds
CountDownAdapter countDown = new CountDownAdapter(60*1000, 1000);
countDown.setSourceActivity(MainActivity.this);
countDown.start();
public void onCountDownTimer(long millisUntilFinished)
{
secondsLefts = millisUntilFinished;
long secondsLeft = millisUntilFinished / 1000;
long hours = secondsLeft / 3600;
long minutes = (secondsLeft % 3600) / 60;
long seconds = secondsLeft % 60;
timerText = hours + "h: "+minutes + "m: "+seconds + "s";
if(timerText.equals("0h: 0m: 1s"))
{
}
timer.setText(timerText);
}
The lifecycle of an app looks like this:
So try to store the current remaining time and the exact current timestamp when onPause() is called (the user switches to another app). And when the user opens the app again, you need to catch the onResume() function.
Read out the stored timestamp and compare it with the current time. The time difference can now be substracted from the remaining time that you also stored.
There might be a better solution but it would be the easiest way to store the timestamp and remaining time in an XML file.
I am a little bit confused by the date formatter/calander and all the formats that are related to the time. I am trying to get the current time with no date and, although I have seen several code snippets this still confuses me.
My problem is the following is to get current time and send it to some method for some calculations.
For example
public void onCreate(Bundle savedInstanceState){....
.........
........
**get the current time**
sendToMethod(currentTime)
}
public void sendToMethod(currentTime)
{
Date pastTime
long diff = currentTime - pastTime;
if (diff > 1000) // here i want to check if the diffrence is more then 10 seconds
// do something
}
I searched and tried several codes but I got lost.
I found that there are alot of ways to do this, but I still have no clue how to solve this.
Please, if anyone can help me I will appreciate it a lot.
Try this:
private long diff, start = System.currentTimeMillis();
public void sendToMethod()
{
diff = System.currentTimeMillis() - start;
if (diff > 10000) { // difference is more then 10 seconds
// do something
}
start = System.currentTimeMillis();
}
You can try using Joda time.
No Joda time:
Calendar c = Calendar.getInstance();
int seconds/minutes/hours/etc = c.get(Calendar.SECOND/MINUTE/HOUR/etc);
In Joda Time:
DateTime dt = new DateTime(DateTimeZone.UTC);
int month = dt.getMonthOfYear();
// where January is 1 and December is 12
int year = dt.getYear();
Edit
If you want to get the current time:
long time = System.currentTimeMillis();
using the current time and the some time in the past you can find the
difference. Time in the past can be obtained via the codes from above.
Joda Time User Guide
public void onCreate(Bundle savedInstanceState) {
long currentTime = System.currentTimeMillis();
sendToMethod(currentTime);
}
public void sendToMethod(long currentTime) {
long diff = currentTime - pastTime;
if (diff > 10000){
// Do some thing here...
}
}
I know here is lot of question on "start/stop timer".But i am looking for format of
timer. I am developing one recording application which show record time.
I able to show time in second format like 1 2 3 4 5. but i need to show this time like
00.01. I need some hint or reference. here is image which i need to show.
Thanks in Advance
Here is the very nice tutorial of simple countdown timer. Go through it and you will be able to achieve what you want.
Or A Stitch in Time is the efficient way to implement the stop watch type app from developer.android.com and yes it uses the format you required.
You can try this:
private String stringForTime(int timeMs) {
StringBuilder mFormatBuilder = new StringBuilder();
Formatter mFormatter = new Formatter(mFormatBuilder, Locale.getDefault());
int totalSeconds = timeMs / 1000;
int seconds = totalSeconds % 60;
int minutes = (totalSeconds / 60) % 60;
int hours = totalSeconds / 3600;
mFormatBuilder.setLength(0);
if (hours > 0) {
return mFormatter.format("%d:%02d:%02d", hours, minutes, seconds).toString();
} else {
return mFormatter.format("%02d:%02d", minutes, seconds).toString();
}
}
i want to run an countdown time , in which i want to show days,hours,sec and milisec remaining for a specific date. and will be be keep changing till the end of the specific date.
Hope you can understand.
Thanks in advance.
Well, I think the problem is, that you dont know, how to work with the time. Here i have a method I use to calculate the amount of time of some items which I parse out of a db.
The param is a double value, which has got the whole time in seconds. It returns a string with the time in days, hours, minutes and seconds as string.
public String timeCalculate(double ttime) {
long days, hours, minutes, seconds;
String daysT = "", restT = "";
days = (Math.round(ttime) / 86400);
hours = (Math.round(ttime) / 3600) - (days * 24);
minutes = (Math.round(ttime) / 60) - (days * 1440) - (hours * 60);
seconds = Math.round(ttime) % 60;
if(days==1) daysT = String.format("%d day ", days);
if(days>1) daysT = String.format("%d days ", days);
restT = String.format("%02d:%02d:%02d", hours, minutes, seconds);
return daysT + restT;
}
For the countdown itself...take the target timestamp minus the actual one and voila, you've got seconds left :) Put those seconds to this method and you've got the remaining time. Now you just need to do some UI things ;)
Oh, and for the usual Unix Timestamp you can use this little method:
public static long getTimestamp() {
return System.currentTimeMillis() / 1000;
}