Java time interval issues for use in Android

Source: Internet
Author: User

Reprint Please specify source: http://www.cnblogs.com/cnwutianhao/p/6606720.html

Suppose we get a piece of audio when we do the project, and we know the length of the audio, then we want to do some processing of the audio, we often have to do the time loop to judge before the processing action.

Known: Audio length, and the following four conditions are also given
Retry Duration = Audio length x0.7 + 30 seconds
First retry: Retry time length x 0.15
Second retry: Retry time length x 0.5
Third retry: Retry time length x 1

We can use the following two code to achieve the known conditions:

Private int[] Getretryintervaltimes (FinalContext Context,Final intduration) {    //Retry Duration = Audio length x0.7 + 30 seconds    Final intRetrymaxtime = Duration * Getresvalue (context, plus)/+ getresvalue (context, 30); //First retry: Retry time length x 0.15//Second retry: Retry time length x 0.5//Third retry: Retry time length 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 (finalfinalint  resId) {    return  Integer.parseint (context.getstring (ResId));}

The execution of the circular judgment, generally is Retry3 times, if not through the processing, three times within, that implementation passed, then began to do processing. The following code can be used to implement:

Private voidInitmethod () {Final int[] Retryintervaltimemillis =Getretryintervaltimes (Getapplicationcontext (), integer audio length); Final Long[] Executetimes = {0, 0, 0, 0}; LongStarttimemillis = 0; //Retry three times     for(inti = 0; I < 4; i++) {        Try {            if(I > 0) {                LongWaittimemillis = Executetimes[i]-System.currenttimemillis (); Date Executetime=NewDate (Executetimes[i]); String Plantime= String.Format (Locale.chinese, "%tf|%tt.%tl", Executetime, Executetime, 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) {                //start of retry time = Confirmation of the first completion time                Final LongRetrystarttimemillis =System.currenttimemillis (); //Retry schedule Start time = Retry time start + retry each time intervalEXECUTETIMES[1] = Retrystarttimemillis + retryintervaltimemillis[1]; executetimes[2] = Retrystarttimemillis + retryintervaltimemillis[2]; executetimes[3] = Retrystarttimemillis + retryintervaltimemillis[3]; } log.d ("", "<" + i + "> [FINISH]. Process time: "+ (System.currenttimemillis ()-Starttimemillis) +" MS "); }    }}

These are the Java time interval issues that are used in Android.

Follow me on Sina Weibo for more information on Android development!

Focus on technology critics, learn about technology, innovation, education and maximizing human intelligence and imagination!

Java time interval issues for use in Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.