English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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; //The application label private Drawable appIcon ; //The application image private Intent intent ; //The Intent to start the application, usually an Activity with Action as Main and Category as Launcher private String pkgName ; //The package name corresponding to the application 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 = 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 ; } }
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, providing a custom view for listView 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); } } }
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 = null; LayoutInflater infater = null ; //Global variable, saves the information of the current queried package 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 information of all applications BrowseApplicationInfoAdapter browseAppAdapter = new BrowseApplicationInfoAdapter( this, mlistAppInfo); listview.setAdapter(browseAppAdapter); listview.setOnItemClickListener(this); } // Click to pop up a dialog to display the size of the package public void onItemClick(AdapterView<63;> arg0, View view, int position, long arg3) { //Update the display of the current package size information 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) ; //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(formatFileSize(cachesize)); tvdatasize.setText(formatFileSize(datasize)) ; tvcodesize.setText(formatFileSize(codesize)) ; tvtotalsize.setText(formatFileSize(totalsize)) ; //Display a custom dialog AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this) ; builder.setView(dialog) ; builder.setTitle(mlistAppInfo.get(position).getAppLabel())+"The size information is:") ; builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-Generated method stub dialog.cancel() ; // Cancel the display of the dialog } }); builder.create().show() ; } public void queryPacakgeSize(String pkgName) throws Exception{ if ( pkgName != null){ //Use reflection mechanism to get the hidden function getPackageSizeInfo of PackageManager class PackageManager pm = getPackageManager(); //Get the pm object try { //Obtain this hidden function through reflection mechanism Method getPackageSizeInfo = pm.getClass().getDeclaredMethod("getPackageSizeInfo", String.class, IPackageStatsObserver.class); //Call this function and assign its parameters, after the calling process is completed, the function of PkgSizeObserver class will be called back getPackageSizeInfo.invoke(pm, pkgName, new PkgSizeObserver()); } catch(Exception ex){ Log.e(TAG, "NoSuchMethodException") ; ex.printStackTrace() ; throw ex ; // Throw an exception } } } //Service class formed by the Binder mechanism of the aidl file public class PkgSizeObserver extends IPackageStatsObserver.Stub{ /*** Callback function, * @param pStatus, the returned data is encapsulated in the PackageStats object * @param succeeded Represents the success of the callback */ @Override public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws 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 of long -String (kb) private String formateFileSize(long size){ return Formatter.formatFileSize(MainActivity.this, size); } // Obtain information about all starting Activities, similar to the Launch interface public void queryAppInfo() { PackageManager pm = this.getPackageManager(); // Obtain PackageManager object Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); // Through the query, obtain all ResolveInfo objects. List<ResolveInfo> resolveInfos = pm.queryIntentActivities(mainIntent, 0); // Call system sorting, sorted by name // This sorting is very important, otherwise only system applications can be displayed, and third-party applications cannot be listed Collections.sort(resolveInfos, new ResolveInfo.DisplayNameComparator(pm)); if (mlistAppInfo != null) { mlistAppInfo.clear(); for (ResolveInfo reInfo : resolveInfos) { String activityName = reInfo.activityInfo.name; // Obtain the name of the starting Activity of the application String pkgName = reInfo.activityInfo.packageName; // Get the application's package name String appLabel = (String) reInfo.loadLabel(pm); // Get the application's Label Drawable icon = reInfo.loadIcon(pm); // Get the application icon // Prepare Intent for the application's starting Activity Intent launchIntent = new Intent(); launchIntent.setComponent(new ComponentName(pkgName,activityName)); // Create an AppInfo object and assign values AppInfo appInfo = new AppInfo(); appInfo.setAppLabel(appLabel); appInfo.setPkgName(pkgName); appInfo.setAppIcon(icon); appInfo.setIntent(launchIntent); mlistAppInfo.add(appInfo); // Add to the list } } } }
The above-mentioned code examples for Android to obtain the size and cache of an application are introduced by the editor for everyone. I hope it will be helpful to everyone. If you have any questions, please leave a message, and the editor will reply to everyone in time. Thank you very much for your support of the Yell Tutorial website!
Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please report by email to codebox.com (replace # with @ when sending an email), and provide relevant evidence. Once verified, this site will immediately delete the infringing content.