Java時間間隔問題在Android中的使用

來源:互聯網
上載者:User

標籤:link   項目   resid   微博   開始時間   間隔   max   新浪微博   nis   

轉載請註明出處:http://www.cnblogs.com/cnwutianhao/p/6606720.html

 

假設我們在做項目的時候,擷取到了一段音頻,也知道音頻長度,那麼我們想對音頻做一些處理的時候,在這個處理動作之前往往要做時間迴圈判斷

已知:音頻長度,並且還給出下面四個條件
重試時間長度 = 音頻長度 × 0.7 + 30秒
第一次重試: 重試時間長度 x 0.15
第二次重試: 重試時間長度 x 0.5
第三次重試: 重試時間長度 x 1

上述的已知條件我們可以用下面這兩段代碼來實現:

private int[] getRetryIntervalTimes(final Context context, final int duration) {    // 重試時間長度 = 音頻長度 × 0.7 + 30秒    final int retryMaxTime = duration * getResValue(context, 70) / 100 + getResValue(context, 30);    // 第一次重試: 重試時間長度 x 0.15    // 第二次重試: 重試時間長度 x 0.5    // 第三次重試: 重試時間長度 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));}

執行迴圈判斷,一般都是Retry3次,如果不通過就不執行處理,三次以內,那次執行通過,那次就開始做處理。可以用下面這段代碼來實現:

private void initMethod() {    final int[] retryIntervalTimeMillis = getRetryIntervalTimes(getApplicationContext(), 整型音頻長度);    final long[] executeTimes = {0, 0, 0, 0};    long startTimeMillis = 0;    // 重試三次    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.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) {                // 重試時間起點 = 首次完成時間確認                final long retryStartTimeMillis = System.currentTimeMillis();                // 重試計劃開始時間 = 重試時間起點 + 重試各次間隔時間                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");        }    }}

以上就是Java時間間隔問題在Android中的使用。

 

關注我的新浪微博,擷取更多Android開發資訊!

關注科技評論家,領略科技、創新、教育以及最大化人類智慧與想象力!

Java時間間隔問題在Android中的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.