Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android清理緩存功能實現

Android清理緩存功能實現

編輯:關於Android編程

我們都知道在Android的設置->應用程序中可以查看應用程序的相關信息,其中有一個功能是清除緩存。如圖:

\

怎麼實現這些功能呢,從Android的setting源碼中可以得到相關信息。

實現如下:

Java代碼:

package com.wang.clearcache;

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;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		
		pm = getPackageManager();
		//反射
		try {
			Method method = PackageManager.class.getMethod("getPackageSizeInfo", new Class[]{String.class,IPackageStatsObserver.class});
			method.invoke(pm, new Object[]{"com.wang.clearcache",new IPackageStatsObserver.Stub() {
				
				@Override
				public 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();
		}
		
	}
}

因為得到緩存信息需要加入android.permission.GET_PACKAGE_SIZE的權限

Androidmainifest.xml




    
    

    
        
            
                

                
            
        
    

因為使用在代碼中使用了PackageManager的getPackageSizeInfo這個函數,但是這個方法是不對外公開的函數,所有我們需要使用發射來調用這個函數,在該方法的內部回調了onGetStatsCompleted(PackageStats pStats, boolean succeeded)這個方法,通過該方法的pStats參數可以得到應用的緩存,數據緩存,代碼容量緩存,在使用的過程中需要用到系統的aidl文件

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;


最後運行的結果:

Z喎?/ym/源碼地址下載:

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

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved