简体   繁体   English

如何使用 Kotlin 清除 android 应用程序中的应用程序数据?

[英]How to clear app data in android app using Kotlin?

I am trying to delete my app data programmatically using Kotlin as my programing language but did not find any solution.我正在尝试使用Kotlin作为我的编程语言以编程方式删除我的应用程序数据,但没有找到任何解决方案。

I have a WebView and it is reloading my URL after a specified time period.我有一个WebView ,它在指定时间段后重新加载我的URL After the specified time is over I want to delete my app data and reload my WebView to delete any stored cache or other files stored from the previous website visit.指定时间结束后,我想删除我的应用程序数据并重新加载我的WebView以删除任何存储的cache或存储从以前的网站访问存储的其他文件。

What I tried so far到目前为止我尝试了什么

package com.technosoftkpk.y_view
import android.os.Bundle
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.appcompat.app.AppCompatActivity
import kotlin.concurrent.thread
import kotlin.random.Random


class MainActivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val myWebView: WebView = findViewById(R.id.webview)
        myWebView.setWebViewClient(WebViewClient())
        myWebView.loadUrl("https://www.technosoft.eu")
        val webSettings = myWebView.settings
        webSettings.javaScriptEnabled = true

        //Random Timer To reload the page
        thread {
            while (true) {
                Thread.sleep(5000)
                // How to add function below to delete app data programmatically after the above time period.
                // function bla bla bla here


                Thread.sleep(5000)
                runOnUiThread { myWebView.reload() }

            }

        }

    }

}

I hope you will understand what I am trying to do.我希望你能理解我想要做什么。 Thanks in advance提前致谢

I want to answer my question here because I found the perfect solution to my problem.我想在这里回答我的问题,因为我找到了解决问题的完美方法。

My function for clearing app data.我的function用于清除应用程序数据。

private fun deleteAppData() {
        try {
            // clearing app data
            val packageName = applicationContext.packageName
            val runtime = Runtime.getRuntime()
            runtime.exec("pm clear $packageName")
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

My call to this function after some time period via thread经过一段时间后,我通过thread调用了这个function

thread {
       while (true) {
       Thread.sleep(5000)
       runOnUiThread { deleteAppData() }
       }
     }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM