国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android獲取應用程序大小和緩存的實例代碼

2019-12-12 04:55:19
字體:
來源:轉載
供稿:網友

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獲取應用程序大小和緩存的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 潜山县| 喜德县| 开化县| 昭平县| 蓬莱市| 左权县| 玉树县| 永兴县| 黄大仙区| 永兴县| 三穗县| 灵石县| 乌恰县| 乡宁县| 新昌县| 衡山县| 舞钢市| 惠安县| 铁岭县| 巴彦县| 高碑店市| 天台县| 玉树县| 额济纳旗| 麦盖提县| 洞口县| 井研县| 新余市| 慈利县| 白朗县| 杭锦旗| 石家庄市| 鄂伦春自治旗| 怀来县| 阿鲁科尔沁旗| 含山县| 朔州市| 阳西县| 鄂尔多斯市| 曲沃县| 章丘市|