ตัวอย่างนี้แสดงวิธีการเปิดใช้งานการบันทึกรหัสผ่านใน WebView โดยใช้ setSavePassword(true) อย่างไรก็ตาม เมธอดนี้ถูก Deprecated แล้วตั้งแต่ API Level 18 (Android 4.3) และถูกลบออกใน API Level 26 (Android 8.0)
⚠️ คำเตือนสำคัญ:
WebSettings.setSavePassword()ถูก Deprecated และไม่ควรใช้ในแอปใหม่- Android ตอนนี้ใช้ Autofill Framework และ Credential Manager แทน
- การเก็บรหัสผ่านใน WebView แบบเก่ามีช่องโหว่ด้านความปลอดภัย
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
เปิด Android Studio ไปที่ File → New Project
ขั้นตอนที่ 2: เพิ่ม Layout 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:gravity="center"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<WebView
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
ขั้นตอนที่ 3: โค้ด 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.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
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 web_view = findViewById(R.id.web_view);
web_view.requestFocus();
web_view.getSettings().setJavaScriptEnabled(true);
// ⚠️ DEPRECATED - ไม่ควรใช้ในโปรดักชัน
// web_view.getSettings().setSavePassword(true);
web_view.loadUrl("https://touch.facebook.com/");
web_view.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
web_view.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
if (progress < 100) {
progressDialog.show();
}
if (progress == 100) {
progressDialog.dismiss();
}
}
});
}
}
วิธีที่แนะนำในปัจจุบัน (Modern Approach)
1. ใช้ Autofill Framework (API 26+)
// ใน AndroidManifest.xml
<application
android:allowBackup="true"
android:fullBackupContent="@xml/backup_rules"
...>
<activity
android:name=".MainActivity"
android:autofillHints="username,password">
...
</activity>
</application>
2. ใช้ Credential Manager (API 33+ / Jetpack)
// build.gradle
dependencies {
implementation "androidx.credentials:credentials:1.2.0"
implementation "androidx.credentials:credentials-play-services-auth:1.2.0"
}
// ใช้ CredentialManager เพื่อจัดการรหัสผ่าน
CredentialManager credentialManager = CredentialManager.create(context);
3. ให้ WebView รองรับ Autofill
WebView webView = findViewById(R.id.web_view); WebSettings settings = webView.getSettings(); settings.setJavaScriptEnabled(true); settings.setDomStorageEnabled(true); // WebView จะทำงานร่วมกับ Autofill Framework โดยอัตโนมัติใน API 26+
ขั้นตอนที่ 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>
การทดสอบ
รันแอปพลิเคชัน (เฉพาะเพื่อศึกษาโค้ดเก่า) จริงควรใช้ Autofill Framework แทน
