Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

解決 Android 幫助文檔打開緩慢的三種方法

編輯:關於 Android 程式設計

開啟 Android SDK 的本地幫助文檔時,若覺得速度慢得讓人抓狂,問題往往出在文檔中的網頁嵌入了會連網載入的 JavaScript。把它註釋掉就能明顯改善。

問題根源

本地 HTML 檔中含有類似下面這行會連網的 script:

<script src="https://www.google.com/jsapi" type="text/javascript"</script>

由於每個頁面都會嘗試連網載入這段程式,離線或網路不佳時自然就慢。將其註釋掉即可解決。

方法一:用 Java 程式批量註釋

以下程式會遞迴走訪 docs 目錄,把每個 HTML 中的那行 script 註釋掉:

package cn.sd.fxd.android;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FormatDoc {
    public static int j = 1;

    public static void main(String[] args) {
        File file = new File("D:/android/android-sdk-windows/docs/");
        searchDirectory(file, 0);
        System.out.println("OVER");
    }

    public static void searchDirectory(File f, int depth) {
        if (!f.isDirectory()) {
            String fileName = f.getName();
            if (fileName.matches(".*?.html")) {
                String src = "<script src=\"https://www.google.com/jsapi\" type=\"text/javascript\"></script>";
                String dst = "<!-- <script src=\"https://www.google.com/jsapi\" type=\"text/javascript\"></script> -->";
                annotation(f, src, dst);
            }
        } else {
            File[] fs = f.listFiles();
            depth++;
            for (int i = 0; i < fs.length; ++i) {
                searchDirectory(fs[i], depth);
            }
        }
    }

    public static void annotation(File f, String src, String dst) {
        String content = read(f);
        content = content.replaceAll(src, dst);
        write(content, f);
        System.out.println(j++);
    }

    public static String read(File src) {
        StringBuffer res = new StringBuffer();
        String line = null;
        try {
            BufferedReader reader = new BufferedReader(new FileReader(src));
            int i = 0;
            while ((line = reader.readLine()) != null) {
                if (i != 0) {
                    res.append('\n');
                }
                res.append(line);
                i++;
            }
            reader.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return res.toString();
    }

    public static boolean write(String cont, File dist) {
        try {
            BufferedWriter writer = new BufferedWriter(new FileWriter(dist));
            writer.write(cont);
            writer.flush();
            writer.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
}

方法二:用一行 shell 指令

其實用 shell 一行就能搞定,比寫 Java 簡潔太多了:

find . -name "*.html" | xargs grep -l "jsapi" | xargs sed -i '/jsapi/d'

方法三:斷網或用離線瀏覽

最偷懶的辦法就是直接斷網,或改用 IE、Firefox 的離線瀏覽模式開啟文檔,避開連網等待即可。

三種方法各有優劣:Java 適合不熟 shell 的人,shell 一行最省事,而斷網則是零成本的應急之計,可依自身情況挑選。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved