Android cache cleanup

Source: Internet
Author: User

Here, cleanup is to clean the cache of other programs in the software, there are two types. Reflection is used in all aspects of the knowledge, there is no way, Android system API to hide these methods. Reflection will not be said, do not know yourself Baidu, Google!

One type with root permission:
Class C1 = Class. forname ("android. content. PM. ipackagedataobserver ");
Method method = PM. getclass (). getmethod ("deleteapplicationcachefiles", String. Class, ipackagedataobserver. Class );
Method. Invoke (PM, packagename, new ipackagedataobserver. Stub (){
@ Override
Public void onremovecompleted (string packagename,
Boolean succeeded) throws RemoteException {
// Todo auto-generated method stub
}
});
<Uses-Permission Android: Name = "android. Permission. delete_cache_files"/>

In addition, you do not need permissions. one-click cleanup:
Private Static long getenvironmentsize ()
{
File localfile = environment. getdatadirectory ();
Long L1;
If (localfile = NULL)
L1 = 0l;
While (true)
{

String STR = localfile. getpath ();
Statfs localstatfs = new statfs (STR );
Long L2 = localstatfs. getblocksize ();
L1 = localstatfs. getblockcount () * L2;
Return L1;
}
}

Packagemanager PM = activity. getpackagemanager ();
Class [] arrayofclass = new class [2];
Class localclass2 = long. type;
Arrayofclass [0] = localclass2;
Arrayofclass [1] = ipackagedataobserver. Class;
Method localmethod = PM. getclass (). getmethod ("freestorageandpolicy", arrayofclass );
Long locallong = long. valueof (getenvironmentsize ()-1l );
Object [] arrayofobject = new object [2];
Arrayofobject [0] = locallong;
Localmethod. Invoke (PM, locallong, new ipackagedataobserver. Stub (){
Public void onremovecompleted (string packagename, Boolean succeeded) throws RemoteException {
// Todo auto-generated method stub
}});
Permission: <uses-Permission Android: Name = "android. Permission. clear_app_cache"/>

This is also a little bit of insight, but it is not very mature. I hope you can learn more about the shortcomings or mistakes.
If ipackagedataobserver cannot be imported, write ipackagedataobserver. aidl as follows:
Package Android. content. PM;

/**
* 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 ipackagedataobserver {
Void onremovecompleted (in string packagename, Boolean succeeded );
} Put It In The SRC package.

 

Related Article

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.