簡體   English   中英

如何檢查代碼是否在后台運行?

[英]How to check whether code is running in background or not?

在Java / Android中,有沒有辦法檢查當前正在執行的代碼行是否在后台線程上執行?

我有一個我正在幻想的小程序,它最終已經達到了全意大利面條的階段。。。這是故意的,因為這樣,如果競爭對手獲得了代碼,然后他們“開放”引擎蓋,”看着它超過20秒鍾后,他們的頭發着火了,他們會尖叫着逃走……但是現在,即使我感到困惑,我也需要以某種方式檢查這種情況。

圖表-A:

// can be called from 1,067 places... some of which are background threads.
public void startDoingAFunDance(String caller, int wobbleIntensity, int spineAngle, int feetSeparationInInches) {

    if (!validCallersForFunDance.contains(caller)) { 
        Log.i("XXX", "Caller not allowed."); 
        return; 
    }

    boolean wasCalledFromBackgroundThread = // ? < what to put here > ?

    Log.i("XXX", "Was startDoingAFunDance() called from a background thread? And the answer is: " + wasCalledFromBackgroundThread);

    // classified

}

一個簡單的方法可能是以下內容

boolean wasCalledFromBackgroundThread = (Thread.currentThread().getId() != 1);

后台線程沒有ID 1(UI線程具有ID)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM