1, put the following two aidl files in their own project, their projects as clients, to achieve cross-process communication.
The code is as follows:
To create a package name:
/***** Copyright, the Android Open Source project**** Licensed under the Apache License, Version 2.0 (the "License"); * * If you are 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** Distribut Ed under the License is distributed on a "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 Pac Kage 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 succ eeded);}
/*//device/java/android/android/view/windowmanager.aidl**** Copyright, the android Open Source project**** Licensed under the Apache License, Version 2.0 (the "License"); * * Do not use this file except in compliance with the License. * * Obtain a copy of the License at * * * * * * * * * * unless required by app Licable law or agreed to in writing, software * * Distributed under the License is distributed on a "as is" BASIS, * * with Out 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 to implement 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;/** * Clean cache * @author YQQ * */public class Cleancacheactivity extends Activity {private Packagemanager pm;private linearlayout mcontainer;// Application to store garbage to be cleaned private ProgressBar mprogressbar;//shows the progress of the scan private TextView tvscanappname;//Application name Private final int scaning_finish=1;//scan app ends private final int scanning=2;//scan//private Mypackageinfo info;private Handler mhandler=new Handler () {@Overridepublic void Handlemessage (Message msg) {switch ( Msg.what) {case SCANING_FINISH:tvScanAppName.setText ("scan Complete"), Break;case scanning://returns information about the application final Mypackageinfo info= (mypackageinfo) msg.obj;//get the application name//string appname=info.packname;tvscanappname.settext ("scanning:" +info.packname); if ( info.cachesize>0) {//Apply the app to the interface container on view view=view.inflate (Cleancacheactivity.this,r.layout.app_cache_info_item, NULL);//Set Event listener view.setclickable (TRUE); View.setbackgroundresource (r.drawable.ache_clear_item_bg_selector);// Empty cache Data View.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {//Open Settings Interface Intent Intent = new I Ntent (); Intent.setaction ("Android.settings.APPLICATION_DETAILS_SETTINGS"); Intent.setdata (Uri.parse ("Package:" + Info.packname)); StartActivity (intent);}); I Mageview 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 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);p m= Getpackagemanager (); Scanappcache ();} /** * Scan app gets the app to be cleaned */private void Scanappcache () {New Thread (new Runnable () {@Overridepublic void run () {List<packagein Fo> infos=pm.getinstalledpackages (0);//Get Information Mprogressbar.setmax (Infos.size ()) of installed applications,//Set the maximum number of progress bars int total=0; for (PackageInfo Info:infos) {getpackagesize (info.packagename); try {thread.sleep); total++; Mprogressbar.setprogress (total);} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} Message Message=message.obtain (); message.what=scaning_finish;//Scan completed mhandler.sendmessage (message);}}). Start ();} /** * Get app size by reflection * @param packname App package name */private void Getpackagesize (String packname) {try {Method 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 package status observer obtains data cache, code size, data size * @author YQQ * */private class Myobserver extends Ipackagestatsobserver.stub{private Str ing packname;//application package name public myobserver (String packname) {super (); this.packname = Packname;} @Overridepublic void ongetstatscompleted (Packagestats pstats, Boolean succeeded) throws RemoteException {// Get the size of each app 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;//app package name long datasize;//data size long cachesize;//cache size}}
Android Aidl Practice cleanup App Cache