下面列出了android.os.Process#THREAD_PRIORITY_FOREGROUND 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public TimeProvider(MediaPlayer mp) {
mPlayer = mp;
try {
getCurrentTimeUs(true, false);
} catch (IllegalStateException e) {
// we assume starting position
mRefresh = true;
}
Looper looper;
if ((looper = Looper.myLooper()) == null &&
(looper = Looper.getMainLooper()) == null) {
// Create our own looper here in case MP was created without one
mHandlerThread = new HandlerThread("MediaPlayerMTPEventThread",
Process.THREAD_PRIORITY_FOREGROUND);
mHandlerThread.start();
looper = mHandlerThread.getLooper();
}
mEventHandler = new EventHandler(looper);
mListeners = new MediaTimeProvider.OnMediaTimeListener[0];
mTimes = new long[0];
mLastTimeUs = 0;
mTimeAdjustment = 0;
}
/**
* Lazily create a handler on a separate thread.
*
* @return the handler
*/
private static Handler getHandler() {
synchronized (sLock) {
if (sHandler == null) {
HandlerThread handlerThread = new HandlerThread("queued-work-looper",
Process.THREAD_PRIORITY_FOREGROUND);
handlerThread.start();
sHandler = new QueuedWorkHandler(handlerThread.getLooper());
}
return sHandler;
}
}
WeatherEngine() {
super();
deviceOrientation = DeviceOrientation.TOP;
handlerThread = new HandlerThread(
String.valueOf(System.currentTimeMillis()),
Process.THREAD_PRIORITY_FOREGROUND
);
handlerThread.start();
handler = new Handler(handlerThread.getLooper());
}
@Override
public void onCreate() {
super.onCreate();
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getSimpleName());
mWakeLock.acquire();
mWorkerThread = new HandlerThread(getClass().getSimpleName(), Process.THREAD_PRIORITY_FOREGROUND);
mWorkerThread.setPriority(Thread.MAX_PRIORITY);
mWorkerThread.start();
mHandler = new Handler(mWorkerThread.getLooper());
}
@Override
public int onStartCommand(final Intent intent, int flags, int startId) {
if (!mInitialized) {
if (intent != null && intent.hasExtra("service")) {
mServiceConfiguration = ServiceConfigurationDTO.fromMap((Map) intent.getSerializableExtra("service"));
}
mWorkerThread = new HandlerThread(getClass().getSimpleName(), Process.THREAD_PRIORITY_FOREGROUND);
mWorkerThread.setPriority(Thread.MAX_PRIORITY);
mWorkerThread.start();
mHandler = new Handler(mWorkerThread.getLooper());
mEmitter = new PjSipBroadcastEmiter(this);
mAudioManager = (AudioManager) getApplicationContext().getSystemService(AUDIO_SERVICE);
mPowerManager = (PowerManager) getApplicationContext().getSystemService(POWER_SERVICE);
mWifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
mWifiLock = mWifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, this.getPackageName()+"-wifi-call-lock");
mWifiLock.setReferenceCounted(false);
mTelephonyManager = (TelephonyManager) getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
mGSMIdle = mTelephonyManager.getCallState() == TelephonyManager.CALL_STATE_IDLE;
IntentFilter phoneStateFilter = new IntentFilter(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
registerReceiver(mPhoneStateChangedReceiver, phoneStateFilter);
mInitialized = true;
job(new Runnable() {
@Override
public void run() {
load();
}
});
}
if (intent != null) {
job(new Runnable() {
@Override
public void run() {
handle(intent);
}
});
}
return START_NOT_STICKY;
}
private UiThread() {
super("android.ui", Process.THREAD_PRIORITY_FOREGROUND, false /*allowIo*/);
}
@Override
public void onCreate() {
Log.i(TAG, "Create version=" + Util.getSelfVersionName(this) + "/" + Util.getSelfVersionCode(this));
startForeground(NOTIFY_WAITING, getWaitingNotification());
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if (jni_context != 0) {
Log.w(TAG, "Create with context=" + jni_context);
jni_stop(jni_context);
synchronized (jni_lock) {
jni_done(jni_context);
jni_context = 0;
}
}
// Native init
jni_context = jni_init(Build.VERSION.SDK_INT);
Log.i(TAG, "Created context=" + jni_context);
boolean pcap = prefs.getBoolean("pcap", false);
setPcap(pcap, this);
prefs.registerOnSharedPreferenceChangeListener(this);
Util.setTheme(this);
super.onCreate();
HandlerThread commandThread = new HandlerThread(getString(R.string.app_name) + " command", Process.THREAD_PRIORITY_FOREGROUND);
HandlerThread logThread = new HandlerThread(getString(R.string.app_name) + " log", Process.THREAD_PRIORITY_BACKGROUND);
HandlerThread statsThread = new HandlerThread(getString(R.string.app_name) + " stats", Process.THREAD_PRIORITY_BACKGROUND);
commandThread.start();
logThread.start();
statsThread.start();
commandLooper = commandThread.getLooper();
logLooper = logThread.getLooper();
statsLooper = statsThread.getLooper();
commandHandler = new CommandHandler(commandLooper);
logHandler = new LogHandler(logLooper);
statsHandler = new StatsHandler(statsLooper);
// Listen for user switches
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
IntentFilter ifUser = new IntentFilter();
ifUser.addAction(Intent.ACTION_USER_BACKGROUND);
ifUser.addAction(Intent.ACTION_USER_FOREGROUND);
registerReceiver(userReceiver, ifUser);
registeredUser = true;
}
// Listen for idle mode state changes
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
IntentFilter ifIdle = new IntentFilter();
ifIdle.addAction(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED);
registerReceiver(idleStateReceiver, ifIdle);
registeredIdleState = true;
}
// Listen for added/removed applications
IntentFilter ifPackage = new IntentFilter();
ifPackage.addAction(Intent.ACTION_PACKAGE_ADDED);
ifPackage.addAction(Intent.ACTION_PACKAGE_REMOVED);
ifPackage.addDataScheme("package");
registerReceiver(packageChangedReceiver, ifPackage);
registeredPackageChanged = true;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
try {
listenNetworkChanges();
} catch (Throwable ex) {
Log.w(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
listenConnectivityChanges();
}
else
listenConnectivityChanges();
// Monitor networks
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
cm.registerNetworkCallback(
new NetworkRequest.Builder()
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET).build(),
networkMonitorCallback);
// Setup house holding
Intent alarmIntent = new Intent(this, ServiceSinkhole.class);
alarmIntent.setAction(ACTION_HOUSE_HOLDING);
PendingIntent pi;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
pi = PendingIntent.getForegroundService(this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
else
pi = PendingIntent.getService(this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.setInexactRepeating(AlarmManager.RTC, SystemClock.elapsedRealtime() + 60 * 1000, AlarmManager.INTERVAL_HALF_DAY, pi);
}
@Override
public void onCreate() {
notificationManager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
Log.i(LOG_TAG, "VPN Create version=" + Util.getSelfVersionName(this) + "/" + Util.getSelfVersionCode(this));
Util.canFilterAsynchronous(this);
if (jni_context != 0) {
Log.w(LOG_TAG, "VPN Create with context=" + jni_context);
jni_stop(jni_context);
synchronized (jni_lock) {
jni_done(jni_context);
jni_context = 0;
}
}
// Native init
jni_context = jni_init(Build.VERSION.SDK_INT);
Log.i(LOG_TAG, "VPN Created context=" + jni_context);
super.onCreate();
HandlerThread commandThread = new HandlerThread(getString(R.string.app_name) + " command", Process.THREAD_PRIORITY_FOREGROUND);
commandThread.start();
commandLooper = commandThread.getLooper();
commandHandler = ServiceVPNHandler.getInstance(commandLooper, this);
// Listen for idle mode state changes
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
IntentFilter ifIdle = new IntentFilter();
ifIdle.addAction(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED);
registerReceiver(idleStateReceiver, ifIdle);
registeredIdleState = true;
}
// Listen for added/removed applications
IntentFilter ifPackage = new IntentFilter();
ifPackage.addAction(Intent.ACTION_PACKAGE_ADDED);
ifPackage.addAction(Intent.ACTION_PACKAGE_REMOVED);
ifPackage.addDataScheme("package");
registerReceiver(packageChangedReceiver, ifPackage);
registeredPackageChanged = true;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
try {
listenNetworkChanges();
} catch (Throwable ex) {
Log.w(LOG_TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
listenConnectivityChanges();
}
} else {
listenConnectivityChanges();
}
}
@IntRange(from = Process.THREAD_PRIORITY_FOREGROUND, to = Process.THREAD_PRIORITY_LOWEST)
int priority();
@Override
public int onStartCommand(final Intent intent, int flags, int startId) {
if (!mInitialized) {
if (intent != null && intent.hasExtra("service")) {
mServiceConfiguration = ServiceConfigurationDTO.fromMap((Map) intent.getSerializableExtra("service"));
}
mWorkerThread = new HandlerThread(getClass().getSimpleName(), Process.THREAD_PRIORITY_FOREGROUND);
mWorkerThread.setPriority(Thread.MAX_PRIORITY);
mWorkerThread.start();
mHandler = new Handler(mWorkerThread.getLooper());
mEmitter = new PjSipBroadcastEmiter(this);
mAudioManager = (AudioManager) getApplicationContext().getSystemService(AUDIO_SERVICE);
mPowerManager = (PowerManager) getApplicationContext().getSystemService(POWER_SERVICE);
mWifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
mWifiLock = mWifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, this.getPackageName()+"-wifi-call-lock");
mWifiLock.setReferenceCounted(false);
mTelephonyManager = (TelephonyManager) getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
mGSMIdle = mTelephonyManager.getCallState() == TelephonyManager.CALL_STATE_IDLE;
IntentFilter phoneStateFilter = new IntentFilter(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
registerReceiver(mPhoneStateChangedReceiver, phoneStateFilter);
mInitialized = true;
job(new Runnable() {
@Override
public void run() {
load();
}
});
}
if (intent != null) {
job(new Runnable() {
@Override
public void run() {
handle(intent);
}
});
}
return START_NOT_STICKY;
}
@Override
public void onCreate() {
Log.i(TAG, "Create version=" + Util.getSelfVersionName(this) + "/" + Util.getSelfVersionCode(this));
startForeground(NOTIFY_WAITING, getWaitingNotification());
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if (jni_context != 0) {
Log.w(TAG, "Create with context=" + jni_context);
jni_stop(jni_context);
synchronized (jni_lock) {
jni_done(jni_context);
jni_context = 0;
}
}
// Native init
jni_context = jni_init(Build.VERSION.SDK_INT);
Log.i(TAG, "Created context=" + jni_context);
boolean pcap = prefs.getBoolean("pcap", false);
setPcap(pcap, this);
prefs.registerOnSharedPreferenceChangeListener(this);
Util.setTheme(this);
super.onCreate();
HandlerThread commandThread = new HandlerThread(getString(R.string.app_name) + " command", Process.THREAD_PRIORITY_FOREGROUND);
HandlerThread logThread = new HandlerThread(getString(R.string.app_name) + " log", Process.THREAD_PRIORITY_BACKGROUND);
HandlerThread statsThread = new HandlerThread(getString(R.string.app_name) + " stats", Process.THREAD_PRIORITY_BACKGROUND);
commandThread.start();
logThread.start();
statsThread.start();
commandLooper = commandThread.getLooper();
logLooper = logThread.getLooper();
statsLooper = statsThread.getLooper();
commandHandler = new CommandHandler(commandLooper);
logHandler = new LogHandler(logLooper);
statsHandler = new StatsHandler(statsLooper);
// Listen for user switches
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
IntentFilter ifUser = new IntentFilter();
ifUser.addAction(Intent.ACTION_USER_BACKGROUND);
ifUser.addAction(Intent.ACTION_USER_FOREGROUND);
registerReceiver(userReceiver, ifUser);
registeredUser = true;
}
// Listen for idle mode state changes
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
IntentFilter ifIdle = new IntentFilter();
ifIdle.addAction(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED);
registerReceiver(idleStateReceiver, ifIdle);
registeredIdleState = true;
}
// Listen for added/removed applications
IntentFilter ifPackage = new IntentFilter();
ifPackage.addAction(Intent.ACTION_PACKAGE_ADDED);
ifPackage.addAction(Intent.ACTION_PACKAGE_REMOVED);
ifPackage.addDataScheme("package");
registerReceiver(packageChangedReceiver, ifPackage);
registeredPackageChanged = true;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
try {
listenNetworkChanges();
} catch (Throwable ex) {
Log.w(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
listenConnectivityChanges();
}
else
listenConnectivityChanges();
// Monitor networks
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
cm.registerNetworkCallback(
new NetworkRequest.Builder()
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET).build(),
networkMonitorCallback);
// Setup house holding
Intent alarmIntent = new Intent(this, ServiceSinkhole.class);
alarmIntent.setAction(ACTION_HOUSE_HOLDING);
PendingIntent pi;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
pi = PendingIntent.getForegroundService(this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
else
pi = PendingIntent.getService(this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.setInexactRepeating(AlarmManager.RTC, SystemClock.elapsedRealtime() + 60 * 1000, AlarmManager.INTERVAL_HALF_DAY, pi);
}
/**
* 优先级的范围,可根据Task重要程度及工作量指定;之后根据实际情况决定是否有必要放更大
*
* @return
*/
@IntRange(from = Process.THREAD_PRIORITY_FOREGROUND, to = Process.THREAD_PRIORITY_LOWEST)
int priority();