Java time interval problem in Android, javaandroid
Reprinted please indicate the source: http://www.cnblogs.com/cnwutianhao/p/6606720.html
Suppose we get an audio clip and know the audio length when we are working on the project. When we want to process the audio, we usually need to make time loop judgment before this processing.
Known: audio length, and the following four conditions are given
Retry duration = audio length x 0.7 + 30 seconds
First retry: length of retry x 0.15
Second retry: The retry duration x 0.5
The third retry: The retry duration x 1
We can use the following two pieces of code to implement the above known conditions:
Private int [] getRetryIntervalTimes (final Context context, final int duration) {// retry duration = audio length × 0.7 + 30 seconds final int retryMaxTime = duration * getResValue (context, 70) /100 + getResValue (context, 30); // first retry: retry duration x 0.15 // second retry: retry duration x 0.5 // third retry: retry duration x 1 return new int [] {0, (retryMaxTime * getResValue (context, 15)/100) * 1000, (retryMaxTime * getResValue (context, 50)/100) * 1000, (retryMaxTime * getResValue (context, 100)/100) * 1000 };}
private int getResValue(final Context context, final int resId) { return Integer.parseInt(context.getString(resId));}
The execution of loop judgment is generally three retrytimes. If it fails, it will not be processed. Within three times, the execution will pass, and the processing will begin. You can use the following code:
Private void initMethod () {final int [] retryIntervalTimeMillis = getRetryIntervalTimes (getApplicationContext (), integer audio length); final long [] executeTimes = {0, 0, 0, 0 }; long startTimeMillis = 0; // retry three times for (int I = 0; I <4; I ++) {try {if (I> 0) {long waitTimeMillis = executeTimes [I]-System. currentTimeMillis (); Date executeTime = new Date (executeTimes [I]); String planTime = String. format (Locale. C HINESE, "% tF | % tT. % tL ", executeTime); Log. d ("", "<" + I + "> Plan:" + planTime + ", Wait:" + (waitTimeMillis> 0? WaitTimeMillis: 0) + "ms"); if (waitTimeMillis> 0) {Thread. sleep (waitTimeMillis);} startTimeMillis = System. currentTimeMillis ();} catch (InterruptedException e) {e. printStackTrace ();} finally {if (I = 0) {// retry time start point = final completion time confirmation final long retryStartTimeMillis = System. currentTimeMillis (); // retry plan start time = retry Start Time + Retry Interval executeTimes [1] = retryStartTimeMillis + retryIntervalTimeMillis [1]; executeTimes [2] = retryStartTimeMillis + retryIntervalTimeMillis [2]; executeTimes [3] = retryStartTimeMillis + retryIntervalTimeMillis [3];} Log. d ("", "<" + I + "> [FINISH]. process time: "+ (System. currentTimeMillis ()-startTimeMillis) + "ms ");}}}
The preceding figure shows how to use Java time interval in Android.
Follow my Sina Weibo website to learn more about Android development!
Focus on science and technology critics, learn about science and technology, innovation, education, and maximize human wisdom and imagination!