Info
Package com.qin.appsize;
Import android.content.Intent;
Import android.graphics.drawable.Drawable; Model class, used to store application information public class AppInfo {private String Applabel;//Application label private drawable AppIcon;//Application Image Private Intent Intent; The intent of the startup application is generally the activity private String pkgname of the action for main and category for Lancher; The package name corresponding to the application is private long cachesize; Cache size private long datasize; Data size private long codesieze; Application size public long getcachesize () {return cachesize.} public void SetCacheSize (long cachesize) {this.cachesize = Cach
Esize; Public long Getdatasize () {return datasize.} public void Setdatasize (long datasize) {this.datasize = datasize;} Publ IC Long Getcodesieze () {return codesieze.} public void Setcodesieze (long codesieze) {this.codesieze = Codesieze;} Publ IC AppInfo () {} public String Getapplabel () {return applabel.} public void Setapplabel (String appName) {This.applabel =
AppName; Public drawable Getappicon () {return appIcon.} public void Setappicon (Drawable appIcon) {This.appicon = AppIcon;} public Intent getintent () {return Intent;} public void Setintent (Intent in Tent) {this.intent = intent} public String Getpkgname () {return pkgname;} public void Setpkgname (String pkgname) {thi
S.pkgname=pkgname; }
}
a custom class
Package com.qin.appsize;
Import java.util.List;
Import Android.content.Context;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.BaseAdapter;
Import Android.widget.ImageView;
Import Android.widget.TextView; Custom adapter class, provided to ListView's Custom view public class Browseapplicationinfoadapter extends Baseadapter {private List<appinfo
> mlistappinfo = null;
Layoutinflater infater = null; Public Browseapplicationinfoadapter (context context, list<appinfo> apps) {infater = (layoutinflater)
Context.getsystemservice (Context.layout_inflater_service);
Mlistappinfo = apps; @Override public int GetCount () {//TODO auto-generated Method Stub System.out.println ("Size" + mlistappinfo.size ());
Eturn mlistappinfo.size ();
@Override public Object getitem (int position) {//TODO auto-generated Method stub return Mlistappinfo.get (position);} @Override public long getitemid (int position) {//TODO auto-generated method stub return 0;
@Override public View getview (int position, View Convertview, ViewGroup arg2) {System.out.println ("GetView at" + posi
tion);
View view = null;
Viewholder holder = null; if (Convertview = null | | Convertview.gettag () = null) {view = Infater.inflate (R.layout.browse_app_item, null); holder
= new Viewholder (view);
View.settag (holder);
} else{view = Convertview Holder = (viewholder) Convertview.gettag ();}
AppInfo AppInfo = (AppInfo) getitem (position);
Holder.appIcon.setImageDrawable (Appinfo.getappicon ());
Holder.tvAppLabel.setText (Appinfo.getapplabel ());
Holder.tvPkgName.setText (Appinfo.getpkgname ());
return view;
Class Viewholder {ImageView AppIcon;
TextView Tvapplabel;
TextView Tvpkgname; Public Viewholder (view view) {This.appicon = (ImageView) View.findviewbyid (r.id.imgapp); This.tvapplabel = (TextView) VI
Ew.findviewbyid (R.id.tvapplabel);
This.tvpkgname = (TextView) View.findviewbyid (r.id.tvpkgname); }
}
}
Main class
Package com.qin.appsize;
Import Java.lang.reflect.Method;
Import java.util.ArrayList;
Import java.util.Collections;
Import java.util.List;
Import Com.qin.appsize.AppInfo;
Import android.app.Activity;
Import Android.app.AlertDialog;
Import Android.content.ComponentName;
Import Android.content.Context;
Import Android.content.DialogInterface;
Import android.content.Intent;
Import Android.content.pm.IPackageStatsObserver;
Import Android.content.pm.PackageManager;
Import Android.content.pm.PackageStats;
Import Android.content.pm.ResolveInfo;
Import android.graphics.drawable.Drawable;
Import Android.os.Bundle;
Import android.os.RemoteException;
Import Android.text.format.Formatter;
Import Android.util.Log;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.widget.AdapterView;
Import Android.widget.ListView;
Import Android.widget.TextView;
Import Android.widget.AdapterView.OnItemClickListener; public class Mainactivity extends activity implements Onitemclicklistener{private static String TAG = ' app_size '; private ListView ListView = null; private list<appinfo> mlistappinfo = nul
L
Layoutinflater infater = null; Global variable, save the current query package information private long cachesize; Cache size private long datasize; Data size private long codesize; Application size private long totalsize; Total size @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (
R.layout.browse_app_list);
ListView = (ListView) Findviewbyid (R.id.listviewapp);
Mlistappinfo = new arraylist<appinfo> (); Queryappinfo ();
Query all application information browseapplicationinfoadapter Browseappadapter = new Browseapplicationinfoadapter (this, mlistappinfo);
Listview.setadapter (Browseappadapter);
Listview.setonitemclicklistener (this); //Click on the pop-up dialog box to display the size of the package public void Onitemclick (adapterview<?> arg0, view view, int position,long arg3) {//update display current packet size letter try {querypacakgesize mlistappinfo.get (position). Getpkgname ()); catch (Exception e) {/TODO auto-generated catch bl OcK E.printstacktrace ();
} Infater = (layoutinflater) MainActivity.this.getSystemService (Context.layout_inflater_service);
View dialog = infater.inflate (r.layout.dialog_app_size, NULL); TextView tvcachesize = (TextView) Dialog.findviewbyid (r.id.tvcachesize); Cache size TextView tvdatasize = (TextView) Dialog.findviewbyid (r.id.tvdatasize); Data size TextView tvcodesize = (TextView) Dialog.findviewbyid (r.id.tvcodesize); Application size TextView tvtotalsize = (TextView) Dialog.findviewbyid (r.id.tvtotalsize);
Total size//type conversion and Assignment Tvcachesize.settext (formatefilesize (cachesize));
Tvdatasize.settext (Formatefilesize (datasize));
Tvcodesize.settext (Formatefilesize (codesize));
Tvtotalsize.settext (Formatefilesize (totalsize));
Displays the custom dialog box Alertdialog.builder Builder =new alertdialog.builder (mainactivity.this);
Builder.setview (Dialog);
Builder.settitle (Mlistappinfo.get (position). Getapplabel () + "The size of the information is:"); Builder.setpositivebutton (OK), new Dialoginterface.onclicklistener () {@Override public void OnClick (DialOginterface dialog, int which) {//TODO auto-generated Method Stub dialog.cancel ();//Cancel Display dialog box}});
Builder.create (). Show (); public void Querypacakgesize (String pkgname) throws exception{if (pkgname!= null) {//The hidden function of the Packagemanager class is obtained using the radiation mechanism GETP Ackagesizeinfo Packagemanager PM = Getpackagemanager (); Get the PM object try {//through the reflection mechanism to get the hidden function method Getpackagesizeinfo = Pm.getclass (). Getdeclaredmethod ("Getpackagesizeinfo",
String.class,ipackagestatsobserver.class);
The function is called and assigned a parameter, and the function of the Pkgsizeobserver class is recalled after the call process completes Getpackagesizeinfo.invoke (PM, pkgname,new pkgsizeobserver ()); catch (Exception ex) {LOG.E (TAG, "nosuchmethodexception"); Ex.printstacktrace (); throw ex;//Throw exception}}//aidl file formed Bindler mechanism service class public class Pkgsizeobserver extends ipackagestatsobserver.stub{/*** callback function, * @param pstatus, return data encapsulated in package Stats Object * @param succeeded Representative callback succeeded/@Override public void ongetstatscompleted (Packagestats pstats, Boolean succeeded) t Hrows remoteexception {//TODO auto-generated method stub CAChesize = pstats.cachesize; Cache size datasize = pstats.datasize; Data size codesize = pstats.codesize;
Application size TotalSize = cachesize + datasize + codesize;
LOG.I (TAG, "CacheSize--->" +cachesize+ "datasize---->" +datasize+ "codesize---->" +codesize); system function, string conversion long-string (KB) private string formatefilesize (long size) {return formatter.formatfilesize (mainactivit
Y.this, size); //Get all startup activity information, similar to the launch interface public void Queryappinfo () {Packagemanager pm = This.getpackagemanager ();//Get Package
Manager Object Intent mainintent = new Intent (intent.action_main, NULL);
Mainintent.addcategory (Intent.category_launcher);
By querying, get all Resolveinfo objects.
list<resolveinfo> Resolveinfos = pm.queryintentactivities (mainintent, 0); Call system sort, sort by name//This sort is important, otherwise you can only display system applications and not list Third-party application Collections.sort (resolveinfos,new
Resolveinfo.displaynamecomparator (PM)); if (mlistappinfo!= null) {mlistappinfo.clear (); for (Resolveinfo Reinfo:resolveinfos) {String activityname = reinfo.a CtivitYinfo.name; Gets the name String Pkgname = ReInfo.activityInfo.packageName of the startup activity for the application; Gets the package name of the application string Applabel = (string) Reinfo.loadlabel (PM); Get the application's label drawable icon = Reinfo.loadicon (PM);
Get application icon//Prepare Intent Intent launchintent = new Intent () for the application's startup activity;
Launchintent.setcomponent (New ComponentName (Pkgname,activityname));
Creates a AppInfo object and assigns the value AppInfo AppInfo = new AppInfo ();
Appinfo.setapplabel (Applabel);
Appinfo.setpkgname (Pkgname);
Appinfo.setappicon (icon);
Appinfo.setintent (launchintent); Mlistappinfo.add (AppInfo); Add to List}}}
The above is a small set to introduce Android to get the application size and cache instance code, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!