एंड्रॉइड WebView में HTML5 डेटाबेस (Web SQL/IndexedDB) इनेबल करना
यह उदाहरण दर्शाता है कि setDatabaseEnabled(true) का उपयोग करके WebView में क्लाइंट-साइड डेटाबेस स्टोरेज कैसे इनेबल करें।
⚠️ महत्वपूर्ण नोट
Web SQL Database डिप्रिकेटेड है और आधुनिक ब्राउज़र्स में हटा दिया गया है। IndexedDB आधुनिक मानक है। Android WebView में setDatabaseEnabled मुख्य रूप से लीगेसी Web SQL के लिए है। IndexedDB के लिए setDomStorageEnabled(true) पर्याप्त है।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में नया प्रोजेक्ट बनाएं।
चरण 2: लेआउट फ़ाइल
<?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>
चरण 3: 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.webkit.WebChromeClient;
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("लोड हो रहा है...");
progressDialog.setCancelable(false);
WebView webView = findViewById(R.id.web_view);
webView.requestFocus();
webView.getSettings().setLightTouchEnabled(true);
// लीगेसी Web SQL डेटाबेस (डिप्रिकेटेड)
webView.getSettings().setDatabaseEnabled(true);
// आधुनिक IndexedDB के लिए - यह आवश्यक है
webView.getSettings().setDomStorageEnabled(true);
webView.loadUrl("https://www.tutorialspoint.com/");
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int progress) {
if (progress < 100) {
progressDialog.show();
}
if (progress == 100) {
progressDialog.dismiss();
}
}
});
}
}
चरण 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>
स्टोरेज प्रकारों की तुलना
| स्टोरेज | स्थिति | WebView सेटिंग |
|---|---|---|
| Web SQL Database | डिप्रिकेटेड | setDatabaseEnabled(true) |
| IndexedDB | आधुनिक मानक | setDomStorageEnabled(true) |
| localStorage | व्यापक समर्थन | setDomStorageEnabled(true) |
| sessionStorage | व्यापक समर्थन | setDomStorageEnabled(true) |
| Cache API | आधुनिक | setAppCacheEnabled(true) |
ऐप चलाएं
ट्यूटोरियलस्पॉइंट लोड होगा - डेटाबेस फीचर्स वाली साइट्स काम करेंगी।
