日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android清除缓存功能来实现

發(fā)布時間:2025/5/22 Android 139 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android清除缓存功能来实现 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

我們都知道在Android的設(shè)置->應(yīng)用程序中能夠查看應(yīng)用程序的相關(guān)信息,當(dāng)中有一個功能是清除緩存。

如圖:


怎么實現(xiàn)這些功能呢,從Android的setting源代碼中能夠得到相關(guān)信息。

實現(xiàn)例如以下:

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;@Overrideprotected 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() {@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();}} }

由于得到緩存信息須要增加android.permission.GET_PACKAGE_SIZE的權(quán)限

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-sdkandroid:minSdkVersion="8"android:targetSdkVersion="17" /><uses-permission android:name="android.permission.GET_PACKAGE_SIZE"/><applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid: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">由于使用在代碼中使用了PackageManager的getPackageSizeInfo這個函數(shù)??墒沁@種方法是不正確外公開的函數(shù),全部我們須要使用發(fā)射來調(diào)用這個函數(shù),在該方法的內(nèi)部回調(diào)了onGetStatsCompleted(PackageStats pStats, boolean succeeded)這種方法,通過該方法的pStats參數(shù)能夠得到應(yīng)用的緩存,數(shù)據(jù)緩存,代碼容量緩存,在使用的過程中須要用到系統(tǒng)的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;

最后執(zhí)行的結(jié)果:

源代碼地址下載:

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

版權(quán)聲明:本文博客原創(chuàng)文章,博客,未經(jīng)同意,不得轉(zhuǎn)載。

總結(jié)

以上是生活随笔為你收集整理的Android清除缓存功能来实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。