消息機制
Handler 機制
Handler 有兩個主要用途:(1)安排 Message 和 runnables 在將來的某個時刻執(zhí)行; (2)將要在不同于自己的線程上執(zhí)行的操作排入隊列。(在多個線程并發(fā)更新UI的同時保證線程安全。)
Android 規(guī)定訪問 UI 只能在主線程中進行,因為 Android 的 UI 控件不是線程安全的,多線程并發(fā)訪問會導(dǎo)致 UI 控件處于不可預(yù)期的狀態(tài)。為什么系統(tǒng)不對 UI 控件的訪問加上鎖機制?缺點有兩個:加鎖會讓 UI 訪問的邏輯變得復(fù)雜;其次鎖機制會降低 UI 訪問的效率。如果子線程訪問 UI,那么程序就會拋出異常。ViewRootImpl 對UI操作做了驗證,這個驗證工作是由 ViewRootImpl的 checkThread 方法完成:
ViewRootImpl.java
void checkThread() {
if (mThread != Thread.currentThread()) {
throw new CalledFromWrongThreadException(
"Only the original thread that created a view hierarchy can touch its views.");
}
}
- Message:Handler 接收和處理的消息對象
- MessageQueue:Message 的隊列,先進先出,每一個線程最多可以擁有一個
- Looper:消息泵,是 MessageQueue 的管理者,會不斷從 MessageQueue 中取出消息,并將消息分給對應(yīng)的 Handler 處理,每個線程只有一個 Looper。
Handler 創(chuàng)建的時候會采用當前線程的 Looper 來構(gòu)造消息循環(huán)系統(tǒng),需要注意的是,線程默認是沒有 Looper 的,直接使用 Handler 會報錯,如果需要使用 Handler 就必須為線程創(chuàng)建 Looper,因為默認的 UI 主線程,也就是 ActivityThread,ActivityThread 被創(chuàng)建的時候就會初始化 Looper,這也是在主線程中默認可以使用 Handler 的原因。
工作原理
ThreadLocal
ThreadLocal 是一個線程內(nèi)部的數(shù)據(jù)存儲類,通過它可以在指定的線程中存儲數(shù)據(jù),其他線程則無法獲取。Looper、ActivityThread 以及 AMS 中都用到了 ThreadLocal。當不同線程訪問同一個ThreadLocal 的 get方法,ThreadLocal 內(nèi)部會從各自的線程中取出一個數(shù)組,然后再從數(shù)組中根據(jù)當前 ThreadLcoal 的索引去查找對應(yīng)的value值。
ThreadLocal.java
public void set(T value) {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null)
map.set(this, value);
else
createMap(t, value);
}
···
public T get() {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null) {
ThreadLocalMap.Entry e = map.getEntry(this);
if (e != null) {
@SuppressWarnings("unchecked")
T result = (T)e.value;
return result;
}
}
return setInitialValue();
}
MessageQueue
MessageQueue主要包含兩個操作:插入和讀取。讀取操作本身會伴隨著刪除操作,插入和讀取對應(yīng)的方法分別是 enqueueMessage 和 next。MessageQueue 內(nèi)部實現(xiàn)并不是用的隊列,實際上通過一個單鏈表的數(shù)據(jù)結(jié)構(gòu)來維護消息列表。next 方法是一個無限循環(huán)的方法,如果消息隊列中沒有消息,那么 next 方法會一直阻塞。當有新消息到來時,next 方法會放回這條消息并將其從單鏈表中移除。
MessageQueue.java
boolean enqueueMessage(Message msg, long when) {
···
synchronized (this) {
···
msg.markInUse();
msg.when = when;
Message p = mMessages;
boolean needWake;
if (p == null || when == 0 || when < p.when) {
// New head, wake up the event queue if blocked.
msg.next = p;
mMessages = msg;
needWake = mBlocked;
} else {
// Inserted within the middle of the queue. Usually we don't have to wake
// up the event queue unless there is a barrier at the head of the queue
// and the message is the earliest asynchronous message in the queue.
needWake = mBlocked && p.target == null && msg.isAsynchronous();
Message prev;
for (;;) {
prev = p;
p = p.next;
if (p == null || when < p.when) {
break;
}
if (needWake && p.isAsynchronous()) {
needWake = false;
}
}
msg.next = p; // invariant: p == prev.next
prev.next = msg;
}
// We can assume mPtr != 0 because mQuitting is false.
if (needWake) {
nativeWake(mPtr);
}
}
return true;
}
···
Message next() {
// Return here if the message loop has already quit and been disposed.
// This can happen if the application tries to restart a looper after quit
// which is not supported.
···
for (;;) {
···
synchronized (this) {
// Try to retrieve the next message. Return if found.
final long now = SystemClock.uptimeMillis();
Message prevMsg = null;
Message msg = mMessages;
if (msg != null && msg.target == null) {
// Stalled by a barrier. Find the next asynchronous message in the queue.
do {
prevMsg = msg;
msg = msg.next;
} while (msg != null && !msg.isAsynchronous());
}
if (msg != null) {
if (now < msg.when) {
// Next message is not ready. Set a timeout to wake up when it is ready.
nextPollTimeoutMillis = (int) Math.min(msg.when - now, Integer.MAX_VALUE);
} else {
// Got a message.
mBlocked = false;
if (prevMsg != null) {
prevMsg.next = msg.next;
} else {
mMessages = msg.next;
}
msg.next = null;
if (DEBUG) Log.v(TAG, "Returning message: " + msg);
msg.markInUse();
return msg;
}
} else {
// No more messages.
nextPollTimeoutMillis = -1;
}
···
}
// Run the idle handlers.
// We only ever reach this code block during the first iteration.
for (int i = 0; i < pendingIdleHandlerCount; i++) {
final IdleHandler idler = mPendingIdleHandlers[i];
mPendingIdleHandlers[i] = null; // release the reference to the handler
boolean keep = false;
try {
keep = idler.queueIdle();
} catch (Throwable t) {
Log.wtf(TAG, "IdleHandler threw exception", t);
}
if (!keep) {
synchronized (this) {
mIdleHandlers.remove(idler);
}
}
}
// Reset the idle handler count to 0 so we do not run them again.
pendingIdleHandlerCount = 0;
// While calling an idle handler, a new message could have been delivered
// so go back and look again for a pending message without waiting.
nextPollTimeoutMillis = 0;
}
}
Looper
Looper 會不停地從 MessageQueue 中 查看是否有新消息,如果有新消息就會立刻處理,否則會一直阻塞。
Looper.java
private Looper(boolean quitAllowed) {
mQueue = new MessageQueue(quitAllowed);
mThread = Thread.currentThread();
}
可通過 Looper.prepare() 為當前線程創(chuàng)建一個 Looper:
new Thread("Thread#2") {
@Override
public void run() {
Looper.prepare();
Handler handler = new Handler();
Looper.loop();
}
}.start();
除了 prepare 方法外,Looper 還提供了 prepareMainLooper 方法,主要是給 ActivityThread 創(chuàng)建 Looper 使用,本質(zhì)也是通過 prepare 方法實現(xiàn)的。由于主線程的 Looper 比較特殊,所以 Looper 提供了一個 getMainLooper 方法來獲取主線程的 Looper。
Looper 提供了 quit 和 quitSafely 來退出一個 Looper,二者的區(qū)別是:quit 會直接退出 Looper,而 quitSafly 只是設(shè)定一個退出標記,然后把消息隊列中的已有消息處理完畢后才安全地退出。Looper 退出后,通過 Handler 發(fā)送的消息會失敗,這個時候 Handler 的 send 方法會返回 false。因此在不需要的時候應(yīng)終止 Looper。
Looper.java
public static void loop() {
final Looper me = myLooper();
if (me == null) {
throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
}
final MessageQueue queue = me.mQueue;
···
for (;;) {
Message msg = queue.next(); // might block
if (msg == null) {
// No message indicates that the message queue is quitting.
return;
}
···
try {
msg.target.dispatchMessage(msg);
dispatchEnd = needEndTime ? SystemClock.uptimeMillis() : 0;
} finally {
if (traceTag != 0) {
Trace.traceEnd(traceTag);
}
}
···
msg.recycleUnchecked();
}
}
loop 方法是一個死循環(huán),唯一跳出循環(huán)的方式是 MessageQueue 的 next 方法返回了null。當 Looper 的 quit 方法被調(diào)用時,Looper就會調(diào)用 MessageQueue 的 quit 或者 qutiSafely 方法來通知消息隊列退出,當消息隊列被標記為退出狀態(tài)時,它的 next 方法就會返回 null。loop 方法會調(diào)用 MessageQueue 的 next 方法來獲取新消息,而 next 是一個阻塞操作,當沒有消息時,next 會一直阻塞,導(dǎo)致 loop 方法一直阻塞。Looper 處理這條消息: msg.target.dispatchMessage(msg),這里的 msg.target 是發(fā)送這條消息的 Handler 對象。
Handler
Handler 的工作主要包含消息的發(fā)送和接收的過程。消息的發(fā)送可以通過 post/send 的一系列方法實現(xiàn),post 最終也是通過send來實現(xiàn)的。
[圖片上傳失敗...(image-8c3163-1589948368199)]
線程異步
應(yīng)用啟動時,系統(tǒng)會為應(yīng)用創(chuàng)建一個名為“主線程”的執(zhí)行線程( UI 線程)。 此線程非常重要,因為它負責(zé)將事件分派給相應(yīng)的用戶界面小部件,其中包括繪圖事件。 此外,它也是應(yīng)用與 Android UI 工具包組件(來自 android.widget 和 android.view 軟件包的組件)進行交互的線程。
系統(tǒng)不會為每個組件實例創(chuàng)建單獨的線程。運行于同一進程的所有組件均在 UI 線程中實例化,并且對每個組件的系統(tǒng)調(diào)用均由該線程進行分派。 因此,響應(yīng)系統(tǒng)回調(diào)的方法(例如,報告用戶操作的 onKeyDown() 或生命周期回調(diào)方法)始終在進程的 UI 線程中運行。
Android 的單線程模式必須遵守兩條規(guī)則:
- 不要阻塞 UI 線程
- 不要在 UI 線程之外訪問 Android UI 工具包
為解決此問題,Android 提供了幾種途徑來從其他線程訪問 UI 線程:
Activity.runOnUiThread(Runnable)View.post(Runnable)View.postDelayed(Runnable, long)
AsyncTask
AsyncTask 封裝了 Thread 和 Handler,并不適合特別耗時的后臺任務(wù),對于特別耗時的任務(wù)來說,建議使用線程池。
基本使用
| 方法 | 說明 |
|---|---|
| onPreExecute() | 異步任務(wù)執(zhí)行前調(diào)用,用于做一些準備工作 |
| doInBackground(Params...params) | 用于執(zhí)行異步任務(wù),此方法中可以通過 publishProgress 方法來更新任務(wù)的進度,publishProgress 會調(diào)用 onProgressUpdate 方法 |
| onProgressUpdate | 在主線程中執(zhí)行,后臺任務(wù)的執(zhí)行進度發(fā)生改變時調(diào)用 |
| onPostExecute | 在主線程中執(zhí)行,在異步任務(wù)執(zhí)行之后 |
import android.os.AsyncTask;
public class DownloadTask extends AsyncTask<String, Integer, Boolean> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Boolean doInBackground(String... strings) {
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
}
}
- 異步任務(wù)的實例必須在 UI 線程中創(chuàng)建,即 AsyncTask 對象必須在UI線程中創(chuàng)建。
- execute(Params... params)方法必須在UI線程中調(diào)用。
- 不要手動調(diào)用 onPreExecute(),doInBackground(),onProgressUpdate(),onPostExecute() 這幾個方法。
- 不能在 doInBackground() 中更改UI組件的信息。
- 一個任務(wù)實例只能執(zhí)行一次,如果執(zhí)行第二次將會拋出異常。
- execute() 方法會讓同一個進程中的 AsyncTask 串行執(zhí)行,如果需要并行,可以調(diào)用 executeOnExcutor 方法。
工作原理
AsyncTask.java
@MainThread
public final AsyncTask<Params, Progress, Result> execute(Params... params) {
return executeOnExecutor(sDefaultExecutor, params);
}
@MainThread
public final AsyncTask<Params, Progress, Result> executeOnExecutor(Executor exec,
Params... params) {
if (mStatus != Status.PENDING) {
switch (mStatus) {
case RUNNING:
throw new IllegalStateException("Cannot execute task:"
+ " the task is already running.");
case FINISHED:
throw new IllegalStateException("Cannot execute task:"
+ " the task has already been executed "
+ "(a task can be executed only once)");
}
}
mStatus = Status.RUNNING;
onPreExecute();
mWorker.mParams = params;
exec.execute(mFuture);
return this;
}
sDefaultExecutor 是一個串行的線程池,一個進程中的所有的 AsyncTask 全部在該線程池中執(zhí)行。AysncTask 中有兩個線程池(SerialExecutor 和 THREAD_POOL_EXECUTOR)和一個 Handler(InternalHandler),其中線程池 SerialExecutor 用于任務(wù)的排隊,THREAD_POOL_EXECUTOR 用于真正地執(zhí)行任務(wù),InternalHandler 用于將執(zhí)行環(huán)境從線程池切換到主線程。
AsyncTask.java
private static Handler getMainHandler() {
synchronized (AsyncTask.class) {
if (sHandler == null) {
sHandler = new InternalHandler(Looper.getMainLooper());
}
return sHandler;
}
}
private static class InternalHandler extends Handler {
public InternalHandler(Looper looper) {
super(looper);
}
@SuppressWarnings({"unchecked", "RawUseOfParameterizedType"})
@Override
public void handleMessage(Message msg) {
AsyncTaskResult<?> result = (AsyncTaskResult<?>) msg.obj;
switch (msg.what) {
case MESSAGE_POST_RESULT:
// There is only one result
result.mTask.finish(result.mData[0]);
break;
case MESSAGE_POST_PROGRESS:
result.mTask.onProgressUpdate(result.mData);
break;
}
}
}
private Result postResult(Result result) {
@SuppressWarnings("unchecked")
Message message = getHandler().obtainMessage(MESSAGE_POST_RESULT,
new AsyncTaskResult<Result>(this, result));
message.sendToTarget();
return result;
}
HandlerThread
HandlerThread 集成了 Thread,卻和普通的 Thread 有顯著的不同。普通的 Thread 主要用于在 run 方法中執(zhí)行一個耗時任務(wù),而 HandlerThread 在內(nèi)部創(chuàng)建了消息隊列,外界需要通過 Handler 的消息方式通知 HanderThread 執(zhí)行一個具體的任務(wù)。
HandlerThread.java
@Override
public void run() {
mTid = Process.myTid();
Looper.prepare();
synchronized (this) {
mLooper = Looper.myLooper();
notifyAll();
}
Process.setThreadPriority(mPriority);
onLooperPrepared();
Looper.loop();
mTid = -1;
}
IntentService
IntentService 可用于執(zhí)行后臺耗時的任務(wù),當任務(wù)執(zhí)行后會自動停止,由于其是 Service 的原因,它的優(yōu)先級比單純的線程要高,所以 IntentService 適合執(zhí)行一些高優(yōu)先級的后臺任務(wù)。在實現(xiàn)上,IntentService 封裝了 HandlerThread 和 Handler。
IntentService.java
@Override
public void onCreate() {
// TODO: It would be nice to have an option to hold a partial wakelock
// during processing, and to have a static startService(Context, Intent)
// method that would launch the service & hand off a wakelock.
super.onCreate();
HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");
thread.start();
mServiceLooper = thread.getLooper();
mServiceHandler = new ServiceHandler(mServiceLooper);
}
IntentService 第一次啟動時,會在 onCreatea 方法中創(chuàng)建一個 HandlerThread,然后使用的 Looper 來構(gòu)造一個 Handler 對象 mServiceHandler,這樣通過 mServiceHandler 發(fā)送的消息最終都會在 HandlerThread 中執(zhí)行。每次啟動 IntentService,它的 onStartCommand 方法就會調(diào)用一次,onStartCommand 中處理每個后臺任務(wù)的 Intent,onStartCommand 調(diào)用了 onStart 方法:
IntentService.java
private final class ServiceHandler extends Handler {
public ServiceHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
onHandleIntent((Intent)msg.obj);
stopSelf(msg.arg1);
}
}
···
@Override
public void onStart(@Nullable Intent intent, int startId) {
Message msg = mServiceHandler.obtainMessage();
msg.arg1 = startId;
msg.obj = intent;
mServiceHandler.sendMessage(msg);
}
可以看出,IntentService 僅僅是通過 mServiceHandler 發(fā)送了一個消息,這個消息會在 HandlerThread 中被處理。mServiceHandler 收到消息后,會將 Intent 對象傳遞給 onHandlerIntent 方法中處理,執(zhí)行結(jié)束后,通過 stopSelf(int startId) 來嘗試停止服務(wù)。(stopSelf() 會立即停止服務(wù),而 stopSelf(int startId) 則會等待所有的消息都處理完畢后才終止服務(wù))。
線程池
線程池的優(yōu)點有以下:
- 重用線程池中的線程,避免因為線程的創(chuàng)建和銷毀帶來性能開銷。
- 能有效控制線程池的最大并發(fā)數(shù),避免大量的線程之間因互相搶占系統(tǒng)資源而導(dǎo)致的阻塞現(xiàn)象。
- 能夠?qū)€程進行管理,并提供定時執(zhí)行以及定間隔循環(huán)執(zhí)行等功能。
java 中,ThreadPoolExecutor 是線程池的真正實現(xiàn):
ThreadPoolExecutor.java
/**
* Creates a new {@code ThreadPoolExecutor} with the given initial
* parameters.
*
* @param corePoolSize 核心線程數(shù)
* @param maximumPoolSize 最大線程數(shù)
* @param keepAliveTime 非核心線程閑置的超時時長
* @param unit 用于指定 keepAliveTime 參數(shù)的時間單位
* @param 任務(wù)隊列,通過線程池的 execute 方法提交的 Runnable 對象會存儲在這個參數(shù)中
* @param threadFactory 線程工廠,用于創(chuàng)建新線程
* @param handler 任務(wù)隊列已滿或者是無法成功執(zhí)行任務(wù)時調(diào)用
*/
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler) {
···
}
| 類型 | 創(chuàng)建方法 | 說明 |
|---|---|---|
| FixedThreadPool | Executors.newFixedThreadPool(int nThreads) | 一種線程數(shù)量固定的線程池,只有核心線程并且不會被回收,沒有超時機制 |
| CachedThreadPool | Executors.newCachedThreadPool() | 一種線程數(shù)量不定的線程池,只有非核心線程,當線程都處于活動狀態(tài)時,會創(chuàng)建新線程來處理新任務(wù),否則會利用空閑的線程,超時時長為60s |
| ScheduledThreadPool | Executors.newScheduledThreadPool(int corePoolSize) | 核心線程數(shù)是固定的,非核心線程數(shù)沒有限制,非核心線程閑置時立刻回收,主要用于執(zhí)行定時任務(wù)和固定周期的重復(fù)任務(wù) |
| SingleThreadExecutor | Executors.newSingleThreadExecutor() | 只有一個核心線程,確保所有任務(wù)在同一線程中按順序執(zhí)行 |
RecyclerView 優(yōu)化
數(shù)據(jù)處理和視圖加載分離:數(shù)據(jù)的處理邏輯盡可能放在異步處理,onBindViewHolder 方法中只處理數(shù)據(jù)填充到視圖中。
數(shù)據(jù)優(yōu)化:分頁拉取遠端數(shù)據(jù),對拉取下來的遠端數(shù)據(jù)進行緩存,提升二次加載速度;對于新增或者刪除數(shù)據(jù)通過 DiffUtil 來進行局部刷新數(shù)據(jù),而不是一味地全局刷新數(shù)據(jù)。
示例
public class AdapterDiffCallback extends DiffUtil.Callback {
private List<String> mOldList;
private List<String> mNewList;
public AdapterDiffCallback(List<String> oldList, List<String> newList) {
mOldList = oldList;
mNewList = newList;
DiffUtil.DiffResult
}
@Override
public int getOldListSize() {
return mOldList.size();
}
@Override
public int getNewListSize() {
return mNewList.size();
}
@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
return mOldList.get(oldItemPosition).getClass().equals(mNewList.get(newItemPosition).getClass());
}
@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
return mOldList.get(oldItemPosition).equals(mNewList.get(newItemPosition));
}
}
DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new AdapterDiffCallback(oldList, newList));
diffResult.dispatchUpdatesTo(mAdapter);
布局優(yōu)化:減少布局層級,簡化 ItemView
升級 RecycleView 版本到 25.1.0 及以上使用 Prefetch 功能
通過重寫 RecyclerView.onViewRecycled(holder) 來回收資源
如果 Item 高度是固定的話,可以使用 RecyclerView.setHasFixedSize(true); 來避免 requestLayout 浪費資源
對 ItemView 設(shè)置監(jiān)聽器,不要對每個 Item 都調(diào)用 addXxListener,應(yīng)該大家公用一個 XxListener,根據(jù) ID 來進行不同的操作,優(yōu)化了對象的頻繁創(chuàng)建帶來的資源消耗
如果多個 RecycledView 的 Adapter 是一樣的,比如嵌套的 RecyclerView 中存在一樣的 Adapter,可以通過設(shè)置 RecyclerView.setRecycledViewPool(pool),來共用一個 RecycledViewPool。
Webview
基本使用
WebView
// 獲取當前頁面的URL
public String getUrl();
// 獲取當前頁面的原始URL(重定向后可能當前url不同)
// 就是http headers的Referer參數(shù),loadUrl時為null
public String getOriginalUrl();
// 獲取當前頁面的標題
public String getTitle();
// 獲取當前頁面的favicon
public Bitmap getFavicon();
// 獲取當前頁面的加載進度
public int getProgress();
// 通知WebView內(nèi)核網(wǎng)絡(luò)狀態(tài)
// 用于設(shè)置JS屬性`window.navigator.isOnline`和產(chǎn)生HTML5事件`online/offline`
public void setNetworkAvailable(boolean networkUp)
// 設(shè)置初始縮放比例
public void setInitialScale(int scaleInPercent);
WebSettings
WebSettings settings = web.getSettings();
// 存儲(storage)
// 啟用HTML5 DOM storage API,默認值 false
settings.setDomStorageEnabled(true);
// 啟用Web SQL Database API,這個設(shè)置會影響同一進程內(nèi)的所有WebView,默認值 false
// 此API已不推薦使用,參考:https://www.w3.org/TR/webdatabase/
settings.setDatabaseEnabled(true);
// 啟用Application Caches API,必需設(shè)置有效的緩存路徑才能生效,默認值 false
// 此API已廢棄,參考:https://developer.mozilla.org/zh-CN/docs/Web/HTML/Using_the_application_cache
settings.setAppCacheEnabled(true);
settings.setAppCachePath(context.getCacheDir().getAbsolutePath());
// 定位(location)
settings.setGeolocationEnabled(true);
// 是否保存表單數(shù)據(jù)
settings.setSaveFormData(true);
// 是否當webview調(diào)用requestFocus時為頁面的某個元素設(shè)置焦點,默認值 true
settings.setNeedInitialFocus(true);
// 是否支持viewport屬性,默認值 false
// 頁面通過`<meta name="viewport" ... />`自適應(yīng)手機屏幕
settings.setUseWideViewPort(true);
// 是否使用overview mode加載頁面,默認值 false
// 當頁面寬度大于WebView寬度時,縮小使頁面寬度等于WebView寬度
settings.setLoadWithOverviewMode(true);
// 布局算法
settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
// 是否支持Javascript,默認值false
settings.setJavaScriptEnabled(true);
// 是否支持多窗口,默認值false
settings.setSupportMultipleWindows(false);
// 是否可用Javascript(window.open)打開窗口,默認值 false
settings.setJavaScriptCanOpenWindowsAutomatically(false);
// 資源訪問
settings.setAllowContentAccess(true); // 是否可訪問Content Provider的資源,默認值 true
settings.setAllowFileAccess(true); // 是否可訪問本地文件,默認值 true
// 是否允許通過file url加載的Javascript讀取本地文件,默認值 false
settings.setAllowFileAccessFromFileURLs(false);
// 是否允許通過file url加載的Javascript讀取全部資源(包括文件,http,https),默認值 false
settings.setAllowUniversalAccessFromFileURLs(false);
// 資源加載
settings.setLoadsImagesAutomatically(true); // 是否自動加載圖片
settings.setBlockNetworkImage(false); // 禁止加載網(wǎng)絡(luò)圖片
settings.setBlockNetworkLoads(false); // 禁止加載所有網(wǎng)絡(luò)資源
// 縮放(zoom)
settings.setSupportZoom(true); // 是否支持縮放
settings.setBuiltInZoomControls(false); // 是否使用內(nèi)置縮放機制
settings.setDisplayZoomControls(true); // 是否顯示內(nèi)置縮放控件
// 默認文本編碼,默認值 "UTF-8"
settings.setDefaultTextEncodingName("UTF-8");
settings.setDefaultFontSize(16); // 默認文字尺寸,默認值16,取值范圍1-72
settings.setDefaultFixedFontSize(16); // 默認等寬字體尺寸,默認值16
settings.setMinimumFontSize(8); // 最小文字尺寸,默認值 8
settings.setMinimumLogicalFontSize(8); // 最小文字邏輯尺寸,默認值 8
settings.setTextZoom(100); // 文字縮放百分比,默認值 100
// 字體
settings.setStandardFontFamily("sans-serif"); // 標準字體,默認值 "sans-serif"
settings.setSerifFontFamily("serif"); // 襯線字體,默認值 "serif"
settings.setSansSerifFontFamily("sans-serif"); // 無襯線字體,默認值 "sans-serif"
settings.setFixedFontFamily("monospace"); // 等寬字體,默認值 "monospace"
settings.setCursiveFontFamily("cursive"); // 手寫體(草書),默認值 "cursive"
settings.setFantasyFontFamily("fantasy"); // 幻想體,默認值 "fantasy"
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// 用戶是否需要通過手勢播放媒體(不會自動播放),默認值 true
settings.setMediaPlaybackRequiresUserGesture(true);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// 5.0以上允許加載http和https混合的頁面(5.0以下默認允許,5.0+默認禁止)
settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// 是否在離開屏幕時光柵化(會增加內(nèi)存消耗),默認值 false
settings.setOffscreenPreRaster(false);
}
if (isNetworkConnected(context)) {
// 根據(jù)cache-control決定是否從網(wǎng)絡(luò)上取數(shù)據(jù)
settings.setCacheMode(WebSettings.LOAD_DEFAULT);
} else {
// 沒網(wǎng),離線加載,優(yōu)先加載緩存(即使已經(jīng)過期)
settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
}
// deprecated
settings.setRenderPriority(WebSettings.RenderPriority.HIGH);
settings.setDatabasePath(context.getDir("database", Context.MODE_PRIVATE).getPath());
settings.setGeolocationDatabasePath(context.getFilesDir().getPath());
WebViewClient
// 攔截頁面加載,返回true表示宿主app攔截并處理了該url,否則返回false由當前WebView處理
// 此方法在API24被廢棄,不處理POST請求
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
// 攔截頁面加載,返回true表示宿主app攔截并處理了該url,否則返回false由當前WebView處理
// 此方法添加于API24,不處理POST請求,可攔截處理子frame的非http請求
@TargetApi(Build.VERSION_CODES.N)
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return shouldOverrideUrlLoading(view, request.getUrl().toString());
}
// 此方法廢棄于API21,調(diào)用于非UI線程
// 攔截資源請求并返回響應(yīng)數(shù)據(jù),返回null時WebView將繼續(xù)加載資源
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
return null;
}
// 此方法添加于API21,調(diào)用于非UI線程
// 攔截資源請求并返回數(shù)據(jù),返回null時WebView將繼續(xù)加載資源
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
return shouldInterceptRequest(view, request.getUrl().toString());
}
// 頁面(url)開始加載
public void onPageStarted(WebView view, String url, Bitmap favicon) {
}
// 頁面(url)完成加載
public void onPageFinished(WebView view, String url) {
}
// 將要加載資源(url)
public void onLoadResource(WebView view, String url) {
}
// 這個回調(diào)添加于API23,僅用于主框架的導(dǎo)航
// 通知應(yīng)用導(dǎo)航到之前頁面時,其遺留的WebView內(nèi)容將不再被繪制。
// 這個回調(diào)可以用來決定哪些WebView可見內(nèi)容能被安全地回收,以確保不顯示陳舊的內(nèi)容
// 它最早被調(diào)用,以此保證WebView.onDraw不會繪制任何之前頁面的內(nèi)容,隨后繪制背景色或需要加載的新內(nèi)容。
// 當HTTP響應(yīng)body已經(jīng)開始加載并體現(xiàn)在DOM上將在隨后的繪制中可見時,這個方法會被調(diào)用。
// 這個回調(diào)發(fā)生在文檔加載的早期,因此它的資源(css,和圖像)可能不可用。
// 如果需要更細粒度的視圖更新,查看 postVisualStateCallback(long, WebView.VisualStateCallback).
// 請注意這上邊的所有條件也支持 postVisualStateCallback(long ,WebView.VisualStateCallback)
public void onPageCommitVisible(WebView view, String url) {
}
// 此方法廢棄于API23
// 主框架加載資源時出錯
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
}
// 此方法添加于API23
// 加載資源時出錯,通常意味著連接不到服務(wù)器
// 由于所有資源加載錯誤都會調(diào)用此方法,所以此方法應(yīng)盡量邏輯簡單
@TargetApi(Build.VERSION_CODES.M)
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
if (request.isForMainFrame()) {
onReceivedError(view, error.getErrorCode(), error.getDescription().toString(), request.getUrl().toString());
}
}
// 此方法添加于API23
// 在加載資源(iframe,image,js,css,ajax...)時收到了 HTTP 錯誤(狀態(tài)碼>=400)
public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
}
// 是否重新提交表單,默認不重發(fā)
public void onFormResubmission(WebView view, Message dontResend, Message resend) {
dontResend.sendToTarget();
}
// 通知應(yīng)用可以將當前的url存儲在數(shù)據(jù)庫中,意味著當前的訪問url已經(jīng)生效并被記錄在內(nèi)核當中。
// 此方法在網(wǎng)頁加載過程中只會被調(diào)用一次,網(wǎng)頁前進后退并不會回調(diào)這個函數(shù)。
public void doUpdateVisitedHistory(WebView view, String url, boolean isReload) {
}
// 加載資源時發(fā)生了一個SSL錯誤,應(yīng)用必需響應(yīng)(繼續(xù)請求或取消請求)
// 處理決策可能被緩存用于后續(xù)的請求,默認行為是取消請求
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.cancel();
}
// 此方法添加于API21,在UI線程被調(diào)用
// 處理SSL客戶端證書請求,必要的話可顯示一個UI來提供KEY。
// 有三種響應(yīng)方式:proceed()/cancel()/ignore(),默認行為是取消請求
// 如果調(diào)用proceed()或cancel(),Webview 將在內(nèi)存中保存響應(yīng)結(jié)果且對相同的"host:port"不會再次調(diào)用 onReceivedClientCertRequest
// 多數(shù)情況下,可通過KeyChain.choosePrivateKeyAlias啟動一個Activity供用戶選擇合適的私鑰
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void onReceivedClientCertRequest(WebView view, ClientCertRequest request) {
request.cancel();
}
// 處理HTTP認證請求,默認行為是取消請求
public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
handler.cancel();
}
// 通知應(yīng)用有個已授權(quán)賬號自動登陸了
public void onReceivedLoginRequest(WebView view, String realm, String account, String args) {
}
// 給應(yīng)用一個機會處理按鍵事件
// 如果返回true,WebView不處理該事件,否則WebView會一直處理,默認返回false
public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
return false;
}
// 處理未被WebView消費的按鍵事件
// WebView總是消費按鍵事件,除非是系統(tǒng)按鍵或shouldOverrideKeyEvent返回true
// 此方法在按鍵事件分派時被異步調(diào)用
public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
super.onUnhandledKeyEvent(view, event);
}
// 通知應(yīng)用頁面縮放系數(shù)變化
public void onScaleChanged(WebView view, float oldScale, float newScale) {
}
WebChromeClient
// 獲得所有訪問歷史項目的列表,用于鏈接著色。
public void getVisitedHistory(ValueCallback<String[]> callback) {
}
// <video /> 控件在未播放時,會展示為一張海報圖,HTML中可通過它的'poster'屬性來指定。
// 如果未指定'poster'屬性,則通過此方法提供一個默認的海報圖。
public Bitmap getDefaultVideoPoster() {
return null;
}
// 當全屏的視頻正在緩沖時,此方法返回一個占位視圖(比如旋轉(zhuǎn)的菊花)。
public View getVideoLoadingProgressView() {
return null;
}
// 接收當前頁面的加載進度
public void onProgressChanged(WebView view, int newProgress) {
}
// 接收文檔標題
public void onReceivedTitle(WebView view, String title) {
}
// 接收圖標(favicon)
public void onReceivedIcon(WebView view, Bitmap icon) {
}
// Android中處理Touch Icon的方案
// http://droidyue.com/blog/2015/01/18/deal-with-touch-icon-in-android/index.html
public void onReceivedTouchIconUrl(WebView view, String url, boolean precomposed) {
}
// 通知應(yīng)用當前頁進入了全屏模式,此時應(yīng)用必須顯示一個包含網(wǎng)頁內(nèi)容的自定義View
public void onShowCustomView(View view, CustomViewCallback callback) {
}
// 通知應(yīng)用當前頁退出了全屏模式,此時應(yīng)用必須隱藏之前顯示的自定義View
public void onHideCustomView() {
}
// 顯示一個alert對話框
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
return false;
}
// 顯示一個confirm對話框
public boolean onJsConfirm(WebView view, String url, String message, JsResult result) {
return false;
}
// 顯示一個prompt對話框
public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {
return false;
}
// 顯示一個對話框讓用戶選擇是否離開當前頁面
public boolean onJsBeforeUnload(WebView view, String url, String message, JsResult result) {
return false;
}
// 指定源的網(wǎng)頁內(nèi)容在沒有設(shè)置權(quán)限狀態(tài)下嘗試使用地理位置API。
// 從API24開始,此方法只為安全的源(https)調(diào)用,非安全的源會被自動拒絕
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
}
// 當前一個調(diào)用 onGeolocationPermissionsShowPrompt() 取消時,隱藏相關(guān)的UI。
public void onGeolocationPermissionsHidePrompt() {
}
// 通知應(yīng)用打開新窗口
public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
return false;
}
// 通知應(yīng)用關(guān)閉窗口
public void onCloseWindow(WebView window) {
}
// 請求獲取取焦點
public void onRequestFocus(WebView view) {
}
// 通知應(yīng)用網(wǎng)頁內(nèi)容申請訪問指定資源的權(quán)限(該權(quán)限未被授權(quán)或拒絕)
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void onPermissionRequest(PermissionRequest request) {
request.deny();
}
// 通知應(yīng)用權(quán)限的申請被取消,隱藏相關(guān)的UI。
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void onPermissionRequestCanceled(PermissionRequest request) {
}
// 為'<input type="file" />'顯示文件選擇器,返回false使用默認處理
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
return false;
}
// 接收JavaScript控制臺消息
public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
return false;
}
Webview 加載優(yōu)化
- 使用本地資源替代
可以 將一些資源文件放在本地的 asset s目錄, 然后重 寫WebViewClient 的 shouldInterceptRequest 方法,對訪問地址進行攔截,當 url 地址命中本地配置的url時,使用本地資源替代,否則就使用網(wǎng)絡(luò)上的資源。
mWebview.setWebViewClient(new WebViewClient() {
// 設(shè)置不用系統(tǒng)瀏覽器打開,
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) { // 如果命中本地資源, 使用本地資源替代
if (mDataHelper.hasLocalResource(url)){
WebResourceResponse response = mDataHelper.getReplacedWebResourceResponse(getApplicationContext(), url);
if (response != null) {
return response;
}
}
return super.shouldInterceptRequest(view, url);
}
@TargetApi(VERSION_CODES.LOLLIPOP)@Override
public WebResourceResponse shouldInterceptRequest(WebView view,WebResourceRequest request) {
String url = request.getUrl().toString();
if (mDataHelper.hasLocalResource(url)) {
WebResourceResponse response = mDataHelper.getReplacedWebResourceResponse(getApplicationContext(), url);
if (response != null) {
return response;
}
}
return super.shouldInterceptRequest(view, request);
}
});
WebView初始化慢,可以在初始化同時先請求數(shù)據(jù),讓后端和網(wǎng)絡(luò)不要閑著。
后端處理慢,可以讓服務(wù)器分trunk輸出,在后端計算的同時前端也加載網(wǎng)絡(luò)靜態(tài)資源。
腳本執(zhí)行慢,就讓腳本在最后運行,不阻塞頁面解析。
同時,合理的預(yù)加載、預(yù)緩存可以讓加載速度的瓶頸更小。
WebView初始化慢,就隨時初始化好一個WebView待用。
DNS和鏈接慢,想辦法復(fù)用客戶端使用的域名和鏈接。
腳本執(zhí)行慢,可以把框架代碼拆分出來,在請求頁面之前就執(zhí)行好。

內(nèi)存泄漏
直接 new WebView 并傳入 application context 代替在 XML 里面聲明以防止 activity 引用被濫用,能解決90+%的 WebView 內(nèi)存泄漏。
vWeb = new WebView(getContext().getApplicationContext());
container.addView(vWeb);
銷毀 WebView
if (vWeb != null) {
vWeb.setWebViewClient(null);
vWeb.setWebChromeClient(null);
vWeb.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
vWeb.clearHistory();
((ViewGroup) vWeb.getParent()).removeView(vWeb);
vWeb.destroy();
vWeb = null;
}