為了提高 我們的Activity中的線程的
線程優先順序(Thread-Priority),我們需要在AndroidManifest.xml 中使用 'uses-permission' 這樣做:
XML:
<uses-permission id="android.permission.RAISED_THREAD_PRIORITY"/>
現在你可以在你的Activity中使用以下代碼改變或提高任何線程的優先順序:
Java:
import android.os.Process;
// ...
// -----------------------------------
// Set the priority of the calling thread, based on Linux priorities:
// -----------------------------------
// Changes the Priority of the calling Thread!
Process.setThreadPriority(12);
// Changes the Priority of passed Thread (first param)
Process.setThreadPriority(Process.myTid(), 12);
這裡 range 的範圍是 -20 (高) 到 +19 (低). 不要選得 太高
最好使用預先定義在 android.os.Process 的constants :
Java:
// Lower is 'more impotant'
Process.THREAD_PRIORITY_LOWEST = 19
Process.THREAD_PRIORITY_BACKGROUND = 5
Process.THREAD_PRIORITY_DEFAULT = 0
Process.THREAD_PRIORITY_FOREGROUND = -5
Process.THREAD_PRIORITY_DISPLAY = -10
Process.THREAD_PRIORITY_URGENT_DISPLAY = -15