主目錄見:Android高級進階知識(這是總目錄索引)
[written by Ticoo]
Handler應用場景
根據前幾篇的分析,根據實際的開發,我們可以總結出以下Handler的使用場景
最簡單的消息發送
主線程使用Handler, 主線程里或子線程里發送消息,或延遲發送消息的方式更新UI
如,
啟動應用時Splash頁面的延遲2,3秒后,跳轉到主頁面
加載完頁面的各個控件后,再加載線程下載圖片,最后更新圖片等等
private static final int WHAT_UPDATE_ICON = 1;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case WHAT_UPDATE_ICON:
Log.e(Tag, "receive message:" + msg.obj);
break;
}
}
};
Message msg = handler.obtainMessage(WHAT_UPDATE_ICON);
msg.obj = "update the imageview";
handler.sendMessage(msg);
使用消息的時候,盡量使用 obtainMessage 的方式來獲取Message,避免多次創建Message對象,消耗內存,效率低下。
結合HandlerThread處理耗時任務
結合HandlerThread,串行的處理單個耗時任務,如單任務下載
class DownloadOneByOne extends HandlerThread {
public DownloadOneByOne() {
super(DownloadOneByOne.class.getSimpleName());
}
@Override
protected void onLooperPrepared() {
super.onLooperPrepared();
// 初始化下載組件
}
}
private HandlerThread mHandlerThread;
private Handler downloadHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
String url = (String) msg.obj;
// 使用下載組件開始下載
}
};
public void initHandler() {
// 初始化Handler
mHandlerThread = new DownloadOneByOne();
mHandlerThread.start();
downloadHandler = new Handler(mHandlerThread.getLooper());
}
private void sendDownloadTask(String downloadUrl) {
// 發送下載任務
Message msg = downloadHandler.obtainMessage(WHAT_DOWNLOAD_TASK);
msg.obj = downloadUrl;
downloadHandler.sendMessage(msg);
}
倒計時View的簡易實現
通過Handler我們還可以快速簡易,并且不占用太多性能的實現一個簡易的倒計時View。
public class CountDownView extends AppCompatTextView {
/**
* 總時間
*/
private long seconds;
/**
* 當前分鐘
*/
private long minutes;
/**
* 當前秒數
*/
private int second = 60;
private static final int SECONDS_PER_MINUTE = 60;
private static final int MILLS_PER_SECOND = 1000;
private static final int MILLS_PER_MINUTE = SECONDS_PER_MINUTE * 1000;
private static final int WHAT_DONE = 2;
private static final int WHAT_TICK = 1;
private int marginEnd;
private StringBuilder content = new StringBuilder();
public CountDownView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
DeviceProfile deviceProfile = Launcher.getLauncher(getContext()).getDeviceProfile();
int size = (int) (MeasureSpec.getSize(widthMeasureSpec) / deviceProfile.inv.numColumns);
marginEnd = marginEnd == 0 ? (size - deviceProfile.iconSizePx) / 2 : marginEnd;
setMarginEnd(marginEnd);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
private void setMarginEnd(int marginEnd) {
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) getLayoutParams();
layoutParams.setMarginEnd(marginEnd);
layoutParams.resolveLayoutDirection(layoutParams.getLayoutDirection());
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
if (handler.hasMessages(WHAT_TICK)) {
handler.removeMessages(WHAT_TICK);
}
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case WHAT_DONE:
setVisibility(View.GONE);
break;
default:
setText(content.toString());
handler.post(runnable);
break;
}
}
};
/***
* 設置倒計時
* @param millis
*/
public void setCountDownMills(long millis) {
seconds = (long) Math.floor(millis / MILLS_PER_SECOND);
minutes = (long) Math.floor(millis / MILLS_PER_MINUTE) - 1;
// start after one second
handler.postDelayed(runnable, MILLS_PER_SECOND);
}
private Runnable runnable = new Runnable() {
@Override
public void run() {
if (seconds <= 0) {
handler.sendEmptyMessage(WHAT_DONE);
return;
}
seconds--;
if (second <= 0) {
second = SECONDS_PER_MINUTE;
minutes = (long) Math.floor(seconds / SECONDS_PER_MINUTE);
}
second--;
content.delete(0, content.length());
appendZeroWhenLower10(minutes);
content.append(":");
appendZeroWhenLower10(second);
if (handler.hasMessages(WHAT_TICK)) {
handler.removeMessages(WHAT_TICK);
}
handler.sendEmptyMessageDelayed(WHAT_TICK, MILLS_PER_SECOND);
}
};
private StringBuilder appendZeroWhenLower10(long value) {
if (value < 10) {
content.append("0").append(value);
} else {
content.append(value);
}
return content;
}
}
結合IntentService的使用
使用IntentService處理耗時的任務相對比較簡單,我們來個有難度的,結合AlarmManager的調度,息屏喚醒IntentService定時處理任務的案例來講
當我們的應用進入后臺activity棧的時候,注冊并啟動AlarmManager任務
private static final String ACTION_WAKE_UP = "com.doze.cpu.wakeup";
public static void registerAlarm(Context context, int wakeType) {
type = wakeType;
if (alarmManager == null)
alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (operation != null) alarmManager.cancel(operation);
schedule(context);
}
private static void schedule(Context context) {
Intent intent = new Intent();
intent.setAction(ACTION_WAKE_UP);
operation = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
switch (type) {
case 0:
AlarmUtils.setRTCWakeup(alarmManager, AlarmUtils.DEFAULT_TRIGGER_AT_MILLIS, operation);
break;
case 1:
AlarmUtils.setElapsedWakeup(alarmManager, AlarmUtils.DEFAULT_TRIGGER_AT_MILLIS, operation);
break;
}
}
定時5分鐘發送一個Action為com.doze.cpu.wakeup的廣播,我們的廣播需要繼承 WakefulBroadcastReceiver, 在onReceive里,調用startWakefulService方法,會創建一個1分鐘的WakeLock,喚醒cpu處理我們的任務,我們的任務在IntentService處理最好不過了,處理完就銷毀,不會有多余的占用
public class WakeCPUReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent wakefulIntent = new Intent(context, WorkService.class);
startWakefulService(context, wakefulIntent);
schedule(context);
}
startWakefulService的源碼
public static ComponentName startWakefulService(Context context, Intent intent) {
synchronized (mActiveWakeLocks) {
int id = mNextId;
mNextId++;
if (mNextId <= 0) {
mNextId = 1;
}
intent.putExtra(EXTRA_WAKE_LOCK_ID, id);
ComponentName comp = context.startService(intent);
if (comp == null) {
return null;
}
PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"wake:" + comp.flattenToShortString());
wl.setReferenceCounted(false);
wl.acquire(60*1000);
mActiveWakeLocks.put(id, wl);
return comp;
}
}
在IntentService里,我們在onHandleIntent處理我們的任務后,再調用
WakefulBroadcastReceiver的靜態方法completeWakefulIntent,釋放WakeLock,減少電量的消耗
public class WorkService extends IntentService {
...
@Override
protected void onHandleIntent(Intent intent) {
Log.e(WakeCPUReceiver.TAG, "WorkService is working");
// TODO 處理我們的任務
WakeCPUReceiver.completeWakefulIntent(intent);
}
}
completeWakefulIntent源碼
public static boolean completeWakefulIntent(Intent intent) {
final int id = intent.getIntExtra(EXTRA_WAKE_LOCK_ID, 0);
if (id == 0) {
return false;
}
synchronized (mActiveWakeLocks) {
PowerManager.WakeLock wl = mActiveWakeLocks.get(id);
if (wl != null) {
wl.release();
mActiveWakeLocks.remove(id);
return true;
}
// We return true whether or not we actually found the wake lock
// the return code is defined to indicate whether the Intent contained
// an identifier for a wake lock that it was supposed to match.
// We just log a warning here if there is no wake lock found, which could
// happen for example if this function is called twice on the same
// intent or the process is killed and restarted before processing the intent.
Log.w("WakefulBroadcastReceiver", "No active wake lock id #" + id);
return true;
}
}
Handler的溢出問題
雖然Handler很好用,但由于它可以延遲發送消息,在我們延遲啟動其他組件,或者使用Activity的引用調用一些方法時,如果在延遲的過程中,Activity finish掉了,這時候就會拋出溢出的異常了。
所以,我們在onDestroy的時候,記得 調用removeCallbacks,removeMessages等移除消息的方法來解決這個問題