Android cache cleanup function implementation, android cache cleanup

Source: Internet
Author: User

Android cache cleanup function implementation, android cache cleanup

We all know that you can view application information in Android settings> applications. One of the functions is to clear the cache.


How can we implement these functions? We can obtain relevant information from the setting source code of Android.

The implementation is as follows:

Java code:

Package com. wang. using ache; import java. lang. reflect. method; import android. OS. bundle; import android. OS. remoteException; import android. app. activity; import android. content. pm. IPackageStatsObserver; import android. content. pm. packageManager; import android. content. pm. packageStats; public class MainActivity extends Activity {private PackageManager pm; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); pm = getPackageManager (); // reflection try {Method = PackageManager. class. getMethod ("getPackageSizeInfo", new Class [] {String. class, IPackageStatsObserver. class}); method. invoke (pm, new Object [] {"com. wang. clearcache ", new IPackageStatsObserver. stub () {@ Overridepublic void onGetStatsCompleted (PackageStats pStats, boolean succeeded) throws RemoteException {long cachesize = pStats. cacheSize; long codesize = pStats. codeSize; long datasize = pStats. dataSize; System. out. println ("cachesize:" + cachesize); System. out. println ("codesize:" + codesize); System. out. println ("datasize" + datasize) ;}}) ;}catch (Exception e) {e. printStackTrace ();}}}

To obtain the cache information, you must grant the permission to android. permission. GET_PACKAGE_SIZE.

Androidmainifest. xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.wang.clearcache"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="17" />    <uses-permission android:name="android.permission.GET_PACKAGE_SIZE"/>    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.wang.clearcache.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>
</pre><pre name="code" class="java">
This is because the getPackageSizeInfo function of PackageManager is used in the code, but this method is not open to the public. All functions need to be called using emission, the onGetStatsCompleted (PackageStats pStats, boolean succeeded) method is called back internally. The pStats parameter of this method can be used to obtain the application cache, data cache, and code capacity cache, the aidl file of the system is used in the process of use.

IPackageStatsObserver:

/***** Copyright 2007, The Android Open Source Project**** Licensed under the Apache License, Version 2.0 (the "License");** you may not use this file except in compliance with the License.** You may obtain a copy of the License at****     http://www.apache.org/licenses/LICENSE-2.0**** Unless required by applicable law or agreed to in writing, software** distributed under the License is distributed on an "AS IS" BASIS,** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.** See the License for the specific language governing permissions and** limitations under the License.*/package android.content.pm;import android.content.pm.PackageStats;/** * API for package data change related callbacks from the Package Manager. * Some usage scenarios include deletion of cache directory, generate * statistics related to code, data, cache usage(TODO) * {@hide} */oneway interface IPackageStatsObserver {        void onGetStatsCompleted(in PackageStats pStats, boolean succeeded);}

PackageStats:

/* //device/java/android/android/view/WindowManager.aidl**** Copyright 2007, The Android Open Source Project**** Licensed under the Apache License, Version 2.0 (the "License"); ** you may not use this file except in compliance with the License. ** You may obtain a copy of the License at ****     http://www.apache.org/licenses/LICENSE-2.0 **** Unless required by applicable law or agreed to in writing, software ** distributed under the License is distributed on an "AS IS" BASIS, ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ** See the License for the specific language governing permissions and ** limitations under the License.*/package android.content.pm;parcelable PackageStats;


The final running result:

Download source code:

Http://download.csdn.net/detail/wangbiaohome/8026535


How can I clear cache for android?

You can use 360 software.

What can I do to clear the cache in Android applications?

It's easy. The cached history files are generally placed under the data/directory.
Of course, it is okay to delete the cached files by yourself. This function is simple to delete files. No other.
After the file is deleted, other functions will be directly executed when the program reads the file without any history records ..

This should be clear the local cache. The format of the local file itself is stored, and there is a special mark. This will be clear to you ..

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.