ภาพรวม
ตัวอย่างนี้สาธิตวิธีการเปิดใช้งาน Application Cache (AppCache) ใน WebView ของ Android โดยใช้ WebSettings.setAppCacheEnabled(true) (หมายเหตุ: AppCache ถูก Deprecated แล้ว ควรใช้ Service Worker + Cache API แทน)
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
เปิด Android Studio ไปที่ File → New Project กรอกข้อมูลที่จำเป็นทั้งหมดเพื่อสร้างโปรเจกต์ใหม่
ขั้นตอนที่ 2: จัดทำ Layout (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<WebView
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
ในโค้ดข้างต้น เราใช้ WebView เต็มหน้าจอเพื่อโหลดเว็บไซต์ tutorialspoint.com
ขั้นตอนที่ 3: เขียนโค้ดหลัก (src/MainActivity.java)
package com.example.myapplication;
import android.app.ProgressDialog;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class MainActivity extends AppCompatActivity {
@RequiresApi(api = Build.VERSION_CODES.P)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading Data...");
progressDialog.setCancelable(false);
WebView webView = findViewById(R.id.web_view);
webView.requestFocus();
webView.getSettings().setLightTouchEnabled(true);
// การตั้งค่าพื้นฐาน
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setGeolocationEnabled(true);
webView.setSoundEffectsEnabled(true);
// เปิดใช้งาน App Cache (Deprecated)
webView.getSettings().setAppCacheEnabled(true);
// ตั้งค่าเส้นทาง Cache (แนะนำ)
// webView.getSettings().setAppCachePath(getCacheDir().getAbsolutePath());
// webView.getSettings().setAppCacheMaxSize(10 * 1024 * 1024); // 10MB
webView.loadUrl("https://www.tutorialspoint.com/");
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
if (newProgress < 100) {
progressDialog.show();
} else {
progressDialog.dismiss();
}
}
});
}
}
คำอธิบายสำคัญ:
setAppCacheEnabled(true)- เปิดใช้งาน HTML5 Application Cache (AppCache)- ⚠ AppCache ถูก Deprecated** - ไม่ควรใช้ในโปรเจกต์ใหม่
- ตัวเลือกเพิ่มเติม (คอมเมนต์ไว้):
setAppCachePath()- กำหนดโฟลเดอร์เก็บ CachesetAppCacheMaxSize()- กำหนดขนาด Cache สูงสุด
Application Cache (AppCache) ถูก Deprecated แล้ว** และถูกลบออกจากมาตรฐาน Web
โค้ดตัวอย่างนี้เป็นเพื่อการศึกษา/รองรับ Legacy เท่านั้น
เทคโนโลยีแทนที่ (Modern Alternative):
• Service Workers + Cache API - มาตรฐานใหม่สำหรับ Offline Support
• Workbox - Library จาก Google ช่วยจัดการ Caching ได้ง่าย
• ต้องใช้
WebViewClient.shouldInterceptRequest() ร่วมกับ Service Worker (ซับซ้อนกว่า)
โค้ดต้นฉบับมีเงื่อนไข
if (progress = = 100) ซึ่งเป็น Syntax Error (ต้องเป็น == ไม่มีช่องว่าง) - ได้แก้ไขแล้ว
ขั้นตอนที่ 4: กำหนดสิทธิ์ใน AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
การทดสอบแอปพลิเคชัน
เชื่อมต่ออุปกรณ์ Android จริงกับคอมพิวเตอร์ ใน Android Studio เปิดไฟล์ Activity แล้วคลิกไอคอน Run จากแถบเครื่องมือ เลือกอุปกรณ์มือถือของคุณ แล้วตรวจสอบหน้าจอ - จะแสดงเว็บไซต์ tutorialspoint.com
