Android AIDL practices: Clearing application cache, androidaidl
1. Put the following two aidl files in your own project. Your project is regarded as a client for cross-process communication.
The Code is as follows:
Create package name:
/***** 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);}
/* //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;
2. Using Java reflection on the activity Interface
Package com. example. yqqmobilesafe. cleanache; import java. lang. reflect. method; import java. util. list; import android. app. activity; import android. content. intent; import android. content. pm. IPackageStatsObserver; import android. content. pm. packageInfo; import android. content. pm. packageManager; import android. content. pm. packageManager. nameNotFoundException; import android. content. pm. packageStats; import android.net. uri; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. OS. remoteException; import android. text. format. formatter; import android. view. view; import android. view. view. onClickListener; import android. widget. imageView; import android. widget. linearLayout; import android. widget. progressBar; import android. widget. textView; import com. example. yqqmobilesafe. r;/*** clear cache * @ author yqq **/public class CleanCacheActivity extends Activity {private PackageManager pm; private LinearLayout mContainer; // private ProgressBar mProgressBar is used to store the application to be cleaned up; // display the Scan progress private TextView tvScanAppName; // Application name private final int SCANING_FINISH = 1; // scan the application to complete private final int SCANNING = 2; // scan in progress // private MyPackageInfo info; private Handler mHandler = new Handler () {@ Overridepublic void handleMessage (Message msg) {switch (msg. what) {case SCANING_FINISH: tvScanAppName. setText ("Scan completed"); break; case SCANNING: // return application information final MyPackageInfo info = (MyPackageInfo) msg. obj; // obtain the application name // String appName = info. packName; tvScanAppName. setText ("scanning:" + info. packName); if (info. cacheSize> 0) {// display the application on the Interface container View = view. inflate (CleanCacheActivity. this, R. layout. app_cache_info_item, null); // sets the event listening view. setClickable (true); view. setBackgroundResource (R. drawable. ache_clear_item_bg_selector); // clear the cache data view. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// enable Intent intent = new Intent (); intent. setAction ("android. settings. APPLICATION_DETAILS_SETTINGS "); intent. setData (Uri. parse ("package:" + info. packName); startActivity (intent) ;}}); ImageView appIconIV = (ImageView) view. findViewById (R. id. iv_app_icon); TextView appNameTV = (TextView) view. findViewById (R. id. TV _app_name); TextView appCacheSizeTV = (TextView) view. findViewById (R. id. TV _app_cache_size); TextView appDataSizeTV = (TextView) view. findViewById (R. id. TV _app_code_size); // set the display data try {appIconIV. setImageDrawable (pm. getApplicationInfo (info. packName, 0 ). loadIcon (pm);} catch (NameNotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace ();} appNameTV. setText (info. packName); appCacheSizeTV. setText ("cache size:" + Formatter. formatFileSize (getApplicationContext (), info. cacheSize); appDataSizeTV. setText ("data size" + Formatter. formatFileSize (getApplicationContext (), info. dataSize); mContainer. addView (view, 0);} break;} super. handleMessage (msg) ;}}; public CleanCacheActivity () {}@ Overrideprotected void onCreate (Bundle savedInstanceState) {setContentView (R. layout. activity_cache_clear); init (); super. onCreate (savedInstanceState);}/*** initialization information */private void init () {mContainer = (LinearLayout) this. findViewById (R. id. ll_container); tvScanAppName = (TextView) this. findViewById (R. id. TV _scaning_app_name); mProgressBar = (ProgressBar) this. findViewById (R. id. pb); pm = getPackageManager (); scanAppCache ();}/*** scan the application to obtain the application to be cleaned */private void scanAppCache () {new Thread (new Runnable () {@ Overridepublic void run () {List <PackageInfo> infos = pm. getInstalledPackages (0); // obtain the information of the installed application mProgressBar. setMax (infos. size (); // set the maximum number of progress bars int total = 0; for (PackageInfo info: infos) {getPackageSize (info. packageName); try {Thread. sleep (300); total ++; mProgressBar. setProgress (total);} catch (InterruptedException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}} Message message = Message. obtain (); message. what = SCANING_FINISH; // scan the mHandler. sendMessage (message );}}). start ();}/*** obtain the application size through reflection * @ param packName application package name */private void getPackageSize (String packName) {try {Method = PackageManager. class. getMethod ("getPackageSizeInfo", new Class [] {String. class, IPackageStatsObserver. class}); method. invoke (pm, new Object [] {packName, new MyObserver (packName)});} catch (Exception e) {// TODO Auto-generated catch blocke. printStackTrace () ;}/ *** defines a packet status observer to obtain the data cache, code size, data size * @ author yqq **/private class MyObserver extends IPackageStatsObserver. stub {private String packName; // application package name public MyObserver (String packName) {super (); this. packName = packName ;}@ Overridepublic void handle (PackageStats pStats, boolean succeeded) throws RemoteException {// obtain the size of each application. MyPackageInfo myPackageInfo = new MyPackageInfo (); myPackageInfo. dataSize = pStats. dataSize; myPackageInfo. cacheSize = pStats. cacheSize; myPackageInfo. packName = packName; Message message = Message. obtain (); message. obj = myPackageInfo; message. what = SCANNING; mHandler. sendMessage (message); myPackageInfo = null ;}} private class MyPackageInfo {String packName; // application package name long dataSize; // data size long cacheSize; // cache size }}