3 years ago (2017-02-23)  Algorithm language |   First to comment  15 
post score 0 times, average 0.0

The Android application will generate some data during operation, such as image cache, database file, configuration file and so on.We may have such a need to clear the cached data in the application when we develop it, allowing the user to choose to delete the data generated within the application. This is a more user-friendly design point.Excessive cache will occupy the storage space of the mobile phone and it is said that it will also reduce the IO performance of the mobile phone. Here's a design that clears the cache provided by the Shum Book Android APP:

How to free Android phone storage

So what data does the Android application generate in operation?Where are they stored?Then we implement the clean function according to the location of the generated data

First of all, simply talk about Android's four data storage methods

1, SharePreferenc

A lightweight data storage method is actually based on the "key-value" key-value pair data stored in the XML file; it is usually used to store some configuration information of the program.

SharePreference storage directory:

It is stored in the "data/data/package name/shared_prefs directory.

SharePreference modify and store data

  • Get the SharedPreference object according to the Context's getSharedPrerences(key, [pattern]) method;
  • Use the editor() method of SharedPreference to get the Editor object;
  • Through the putXXX() method of the Editor, the key value pairs store data;
  • Submit data to SharedPreference via Editor's commit() method

2, SQLite

SQLite is a lightweight relational database. Since it is a relational database, it actually operates in much the same way as mysql and sql server.

Android provides a powerful database management library, but also supports the operation of the original command, the addition, deletion, and inspection of the database

3, File

Android can create files for saving data on the device's own storage device or an external storage device.By default, files cannot be shared between different programs.

File can operate on files through FileInputStream and FileOutputStream.

4, ContentProvider

ContentProvider is more complicated than other methods, of course, its function is also a revolutionary change compared to other methods.It enables data manipulation across applications.Using the ContentResolver object's delete, update, insert, query and other methods to manipulate the ContentProvider object, let the ContentProvider object's method to operate on the data.

The implementation is:

  • Defining a ContentProvider in an A program, overloading its methods for adding, deleting, checking, and modifying;
  • Registering ContentProvider in AndroidManifest.xml in A program;
  • In the B program through the ContentResolver and Uri to get ContentProvider data, also use the Resolver additions and deletions to change and retrieve methods to obtain and process data

Android application data storage path

All paths and corresponding data cleanup code for in-app data:

  • /data/data/com.xxx.xxx/cache - In-app cache (Note: corresponding method getCacheDir() )
     
  • /data/data/com.xxx.xxx/databases - In-app database
     
  • /data/data/com.xxx.xxx/shared_prefs - In-app configuration file

 

  • /data/data/com.xxx.xxx/files - In-application files (Note: The corresponding method getFilesDir())

  Due to the wide range of Android application data storage permissions, an Android application can create a directory and file anywhere on the SD card, so the above storage paths are just the most basic storage path for Android applications.More removal code implementation will be explained in more detail in the following removal tool class

Clearing tool class code implementation

For ease of use, the following data cleanup tool classes I packaged:

 

System implementation application management "clear data" method

In the application management of the Android system settings, the "clear data" function is provided. This function completely eliminates the application data. We can achieve similar requirements in the code, but the following code execution will make the application exit:

 

 

This article has been printed on copyright and is protected by copyright laws. It must not be reproduced without permission.If you need to reprint, please contact the author or visit the copyright to obtain the authorization. If you feel that this article is useful to you, you can click the "Sponsoring Author" below to call the author!

Reprinted Note Source: Baiyuan's Blog>>https://wangbaiyuan.cn/en/how-release-android-mobile-phone-storage-space-2.html

Post comment

Style

No Comment

登录

Forget password?

您也可以使用第三方帐号快捷登录

切换登录

注册

TW