Android获取应用程序大小和缓存的实例代码
程序员文章站
2024-03-02 23:26:16
info
package com.qin.appsize;
import android.content.intent;
import android.gra...
info
package com.qin.appsize; import android.content.intent; import android.graphics.drawable.drawable; //model类 ,用来存储应用程序信息 public class appinfo { private string applabel; //应用程序标签 private drawable appicon ; //应用程序图像 private intent intent ; //启动应用程序的intent ,一般是action为main和category为lancher的activity private string pkgname ; //应用程序所对应的包名 private long cachesize ; //缓存大小 private long datasize ; //数据大小 private long codesieze ; //应用程序大小 public long getcachesize() { return cachesize; } public void setcachesize(long cachesize) { this.cachesize = cachesize; } public long getdatasize() { return datasize; } public void setdatasize(long datasize) { this.datasize = datasize; } public long getcodesieze() { return codesieze; } public void setcodesieze(long codesieze) { this.codesieze = codesieze; } public 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 intent) { this.intent = intent; } public string getpkgname(){ return pkgname ; } public void setpkgname(string pkgname){ this.pkgname=pkgname ; } }
自定义的类
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; //自定义适配器类,提供给listview的自定义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()); return 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 " + position); 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) view.findviewbyid(r.id.tvapplabel); this.tvpkgname = (textview) view.findviewbyid(r.id.tvpkgname); } } }
主类
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 = null; layoutinflater infater = null ; //全局变量,保存当前查询包得信息 private long cachesize ; //缓存大小 private long datasize ; //数据大小 private long codesize ; //应用程序大小 private long totalsize ; //总大小 @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(); // 查询所有应用程序信息 browseapplicationinfoadapter browseappadapter = new browseapplicationinfoadapter( this, mlistappinfo); listview.setadapter(browseappadapter); listview.setonitemclicklistener(this); } // 点击弹出对话框,显示该包得大小 public void onitemclick(adapterview<?> arg0, view view, int position,long arg3) { //更新显示当前包得大小信息 try { querypacakgesize(mlistappinfo.get(position).getpkgname()); } catch (exception e) { // todo auto-generated catch block 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) ; //缓存大小 textview tvdatasize = (textview) dialog.findviewbyid(r.id.tvdatasize) ; //数据大小 textview tvcodesize = (textview) dialog.findviewbyid(r.id.tvcodesize) ; // 应用程序大小 textview tvtotalsize = (textview) dialog.findviewbyid(r.id.tvtotalsize) ; //总大小 //类型转换并赋值 tvcachesize.settext(formatefilesize(cachesize)); tvdatasize.settext(formatefilesize(datasize)) ; tvcodesize.settext(formatefilesize(codesize)) ; tvtotalsize.settext(formatefilesize(totalsize)) ; //显示自定义对话框 alertdialog.builder builder =new alertdialog.builder(mainactivity.this) ; builder.setview(dialog) ; builder.settitle(mlistappinfo.get(position).getapplabel()+"的大小信息为:") ; builder.setpositivebutton("确定", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { // todo auto-generated method stub dialog.cancel() ; // 取消显示对话框 } }); builder.create().show() ; } public void querypacakgesize(string pkgname) throws exception{ if ( pkgname != null){ //使用放射机制得到packagemanager类的隐藏函数getpackagesizeinfo packagemanager pm = getpackagemanager(); //得到pm对象 try { //通过反射机制获得该隐藏函数 method getpackagesizeinfo = pm.getclass().getdeclaredmethod("getpackagesizeinfo", string.class,ipackagestatsobserver.class); //调用该函数,并且给其分配参数 ,待调用流程完成后会回调pkgsizeobserver类的函数 getpackagesizeinfo.invoke(pm, pkgname,new pkgsizeobserver()); } catch(exception ex){ log.e(tag, "nosuchmethodexception") ; ex.printstacktrace() ; throw ex ; // 抛出异常 } } } //aidl文件形成的bindler机制服务类 public class pkgsizeobserver extends ipackagestatsobserver.stub{ /*** 回调函数, * @param pstatus ,返回数据封装在packagestats对象中 * @param succeeded 代表回调成功 */ @override public void ongetstatscompleted(packagestats pstats, boolean succeeded) throws remoteexception { // todo auto-generated method stub cachesize = pstats.cachesize ; //缓存大小 datasize = pstats.datasize ; //数据大小 codesize = pstats.codesize ; //应用程序大小 totalsize = cachesize + datasize + codesize ; log.i(tag, "cachesize--->"+cachesize+" datasize---->"+datasize+ " codesize---->"+codesize) ; } } //系统函数,字符串转换 long -string (kb) private string formatefilesize(long size){ return formatter.formatfilesize(mainactivity.this, size); } // 获得所有启动activity的信息,类似于launch界面 public void queryappinfo() { packagemanager pm = this.getpackagemanager(); // 获得packagemanager对象 intent mainintent = new intent(intent.action_main, null); mainintent.addcategory(intent.category_launcher); // 通过查询,获得所有resolveinfo对象. list<resolveinfo> resolveinfos = pm.queryintentactivities(mainintent, 0); // 调用系统排序 , 根据name排序 // 该排序很重要,否则只能显示系统应用,而不能列出第三方应用程序 collections.sort(resolveinfos,new resolveinfo.displaynamecomparator(pm)); if (mlistappinfo != null) { mlistappinfo.clear(); for (resolveinfo reinfo : resolveinfos) { string activityname = reinfo.activityinfo.name; // 获得该应用程序的启动activity的name string pkgname = reinfo.activityinfo.packagename; // 获得应用程序的包名 string applabel = (string) reinfo.loadlabel(pm); // 获得应用程序的label drawable icon = reinfo.loadicon(pm); // 获得应用程序图标 // 为应用程序的启动activity 准备intent intent launchintent = new intent(); launchintent.setcomponent(new componentname(pkgname,activityname)); // 创建一个appinfo对象,并赋值 appinfo appinfo = new appinfo(); appinfo.setapplabel(applabel); appinfo.setpkgname(pkgname); appinfo.setappicon(icon); appinfo.setintent(launchintent); mlistappinfo.add(appinfo); // 添加至列表中 } } } }
以上所述是小编给大家介绍的android获取应用程序大小和缓存的实例代码,希望对大家有所帮助