如何在安卓中清除 Web 檢視歷史記錄?


此示例展示瞭如何在安卓中清除 Web 檢視歷史記錄。

步驟 1 − 在 Android Studio 中新建一個專案,轉到檔案 ⇒ 新建專案,填寫所有必要資訊新建一個專案。

步驟 2 − 在 res/layout/activity_main.xml 中新增以下程式碼。

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
   xmlns:app = "http://schemas.android.com/apk/res-auto"
   xmlns:tools = "http://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>

在以上程式碼中,我們已採用 Web 檢視來顯示 mylocation.org。

步驟 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.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.EditText;
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().setLightTouchEnabled(true);
      web_view.getSettings().setJavaScriptEnabled(true);
      web_view.getSettings().setGeolocationEnabled(true);
      web_view.clearHistory();
      web_view.loadUrl("https://mylocation.org/");
      web_view.setWebChromeClient(new WebChromeClient() {
         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 = "http://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 執行應用程式,開啟你的一個專案活動檔案,再從工具欄中單擊執行  圖示。選擇你的移動裝置作為選項,然後檢視顯示預設畫面的移動裝置 -

點選 此處 下載專案程式碼

更新於:2019 年 7 月 30 日

1000+ 瀏覽

開啟您的 職業生涯

完成本課程以獲得認證

開始吧
廣告
© . All rights reserved.