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的Activityprivate 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的自定义viewpublic 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 ;}@Overridepublic int getCount() {// TODO Auto-generated method stubSystem.out.println("size" + mlistAppInfo.size());return mlistAppInfo.size();}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn mlistAppInfo.get(position);}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn 0;}@Overridepublic 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 ; //总大小@Overridepublic 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 blocke.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() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubdialog.cancel() ; // 取消显示对话框}});builder.create().show() ;}public void queryPacakgeSize(String pkgName) throws Exception{if ( pkgName != null){//使用放射机制得到PackageManager类的隐藏函数getPackageSizeInfoPackageManager 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 代表回调成功*/ @Overridepublic void onGetStatsCompleted(PackageStats pStats, boolean succeeded)throws RemoteException {// TODO Auto-generated method stubcachesize = 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的nameString pkgName = reInfo.activityInfo.packageName; // 获得应用程序的包名String appLabel = (String) reInfo.loadLabel(pm); // 获得应用程序的LabelDrawable icon = reInfo.loadIcon(pm); // 获得应用程序图标// 为应用程序的启动Activity 准备IntentIntent 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获取应用程序大小和缓存的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!
新闻热点
疑难解答