欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android獲取應(yīng)用程序大小和緩存的實(shí)例代碼

 更新時(shí)間:2016年10月27日 14:36:53   作者:大企鵝王  
這篇文章主要介紹了Android獲取應(yīng)用程序大小和緩存的實(shí)例代碼的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下

info

package com.qin.appsize;
import android.content.Intent;
import android.graphics.drawable.Drawable;
//Model類 ,用來(lái)存儲(chǔ)應(yīng)用程序信息
public class AppInfo {
private String appLabel; //應(yīng)用程序標(biāo)簽
private Drawable appIcon ; //應(yīng)用程序圖像
private Intent intent ; //啟動(dòng)應(yīng)用程序的Intent ,一般是Action為Main和Category為L(zhǎng)ancher的Activity
private String pkgName ; //應(yīng)用程序所對(duì)應(yīng)的包名
private long cachesize ; //緩存大小
private long datasize ; //數(shù)據(jù)大小
private long codesieze ; //應(yīng)用程序大小
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 ; 
//全局變量,保存當(dāng)前查詢包得信息
private long cachesize ; //緩存大小
private long datasize ; //數(shù)據(jù)大小 
private long codesize ; //應(yīng)用程序大小
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(); // 查詢所有應(yīng)用程序信息
BrowseApplicationInfoAdapter browseAppAdapter = new BrowseApplicationInfoAdapter(
this, mlistAppInfo);
listview.setAdapter(browseAppAdapter);
listview.setOnItemClickListener(this);
}
// 點(diǎn)擊彈出對(duì)話框,顯示該包得大小
public void onItemClick(AdapterView<?> arg0, View view, int position,long arg3) {
//更新顯示當(dāng)前包得大小信息
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) ; //數(shù)據(jù)大小
TextView tvcodesize = (TextView) dialog.findViewById(R.id.tvcodesize) ; // 應(yīng)用程序大小
TextView tvtotalsize = (TextView) dialog.findViewById(R.id.tvtotalsize) ; //總大小
//類型轉(zhuǎn)換并賦值
tvcachesize.setText(formateFileSize(cachesize));
tvdatasize.setText(formateFileSize(datasize)) ;
tvcodesize.setText(formateFileSize(codesize)) ;
tvtotalsize.setText(formateFileSize(totalsize)) ;
//顯示自定義對(duì)話框
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() ; // 取消顯示對(duì)話框
}
});
builder.create().show() ;
}
public void queryPacakgeSize(String pkgName) throws Exception{
if ( pkgName != null){
//使用放射機(jī)制得到PackageManager類的隱藏函數(shù)getPackageSizeInfo
PackageManager pm = getPackageManager(); //得到pm對(duì)象
try {
//通過(guò)反射機(jī)制獲得該隱藏函數(shù)
Method getPackageSizeInfo = pm.getClass().getDeclaredMethod("getPackageSizeInfo", String.class,IPackageStatsObserver.class);
//調(diào)用該函數(shù),并且給其分配參數(shù) ,待調(diào)用流程完成后會(huì)回調(diào)PkgSizeObserver類的函數(shù)
getPackageSizeInfo.invoke(pm, pkgName,new PkgSizeObserver());
} 
catch(Exception ex){
Log.e(TAG, "NoSuchMethodException") ;
ex.printStackTrace() ;
throw ex ; // 拋出異常
} 
}
}
//aidl文件形成的Bindler機(jī)制服務(wù)類
public class PkgSizeObserver extends IPackageStatsObserver.Stub{
/*** 回調(diào)函數(shù),
* @param pStatus ,返回?cái)?shù)據(jù)封裝在PackageStats對(duì)象中
* @param succeeded 代表回調(diào)成功
*/ 
@Override
public void onGetStatsCompleted(PackageStats pStats, boolean succeeded)
throws RemoteException {
// TODO Auto-generated method stub
cachesize = pStats.cacheSize ; //緩存大小
datasize = pStats.dataSize ; //數(shù)據(jù)大小 
codesize = pStats.codeSize ; //應(yīng)用程序大小
totalsize = cachesize + datasize + codesize ;
Log.i(TAG, "cachesize--->"+cachesize+" datasize---->"+datasize+ " codeSize---->"+codesize) ;
}
}
//系統(tǒng)函數(shù),字符串轉(zhuǎn)換 long -String (kb)
private String formateFileSize(long size){
return Formatter.formatFileSize(MainActivity.this, size); 
}
// 獲得所有啟動(dòng)Activity的信息,類似于Launch界面
public void queryAppInfo() {
PackageManager pm = this.getPackageManager(); // 獲得PackageManager對(duì)象
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
// 通過(guò)查詢,獲得所有ResolveInfo對(duì)象.
List<ResolveInfo> resolveInfos = pm.queryIntentActivities(mainIntent, 0);
// 調(diào)用系統(tǒng)排序 , 根據(jù)name排序
// 該排序很重要,否則只能顯示系統(tǒng)應(yīng)用,而不能列出第三方應(yīng)用程序
Collections.sort(resolveInfos,new ResolveInfo.DisplayNameComparator(pm));
if (mlistAppInfo != null) {
mlistAppInfo.clear();
for (ResolveInfo reInfo : resolveInfos) {
String activityName = reInfo.activityInfo.name; // 獲得該應(yīng)用程序的啟動(dòng)Activity的name
String pkgName = reInfo.activityInfo.packageName; // 獲得應(yīng)用程序的包名
String appLabel = (String) reInfo.loadLabel(pm); // 獲得應(yīng)用程序的Label
Drawable icon = reInfo.loadIcon(pm); // 獲得應(yīng)用程序圖標(biāo)
// 為應(yīng)用程序的啟動(dòng)Activity 準(zhǔn)備Intent
Intent launchIntent = new Intent();
launchIntent.setComponent(new ComponentName(pkgName,activityName));
// 創(chuàng)建一個(gè)AppInfo對(duì)象,并賦值
AppInfo appInfo = new AppInfo();
appInfo.setAppLabel(appLabel);
appInfo.setPkgName(pkgName);
appInfo.setAppIcon(icon);
appInfo.setIntent(launchIntent);
mlistAppInfo.add(appInfo); // 添加至列表中
}
}
}
}

以上所述是小編給大家介紹的Android獲取應(yīng)用程序大小和緩存的實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • 5分鐘學(xué)會(huì)Android設(shè)計(jì)模式之策略模式Strategy Pattern教程

    5分鐘學(xué)會(huì)Android設(shè)計(jì)模式之策略模式Strategy Pattern教程

    這篇文章主要為大家介紹了5分鐘學(xué)會(huì)Android設(shè)計(jì)模式之策略模式Strategy Pattern教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • Android如何自定義EditText光標(biāo)與下劃線顏色詳解

    Android如何自定義EditText光標(biāo)與下劃線顏色詳解

    在android開(kāi)發(fā)中 EditTextText是我們經(jīng)常用到的,我們使用時(shí)會(huì)有一些小問(wèn)題,下面這篇文章主要給大家介紹了關(guān)于利用Android如何自定義EditText光標(biāo)與下劃線顏色的相關(guān)資料,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2017-08-08
  • RecyclerView實(shí)現(xiàn)橫向滾動(dòng)效果

    RecyclerView實(shí)現(xiàn)橫向滾動(dòng)效果

    這篇文章主要為大家詳細(xì)介紹了RecyclerView實(shí)現(xiàn)橫向滾動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-01-01
  • Android自定義狀態(tài)欄顏色與應(yīng)用標(biāo)題欄顏色一致

    Android自定義狀態(tài)欄顏色與應(yīng)用標(biāo)題欄顏色一致

    看IOS上的應(yīng)用,應(yīng)用中狀態(tài)欄的顏色總能與應(yīng)用標(biāo)題欄顏色保持一致,用戶體驗(yàn)很不錯(cuò),對(duì)于這種效果怎么實(shí)現(xiàn)的呢?下面小編給大家分享android自定義狀態(tài)欄顏色與應(yīng)用標(biāo)題欄顏色一致的實(shí)現(xiàn)方法,一起看看吧
    2016-09-09
  • Android Flutter實(shí)現(xiàn)淘寶App的搜索推薦

    Android Flutter實(shí)現(xiàn)淘寶App的搜索推薦

    這篇文章主要為大家詳細(xì)介紹了Android?Flutter如何模擬實(shí)現(xiàn)淘寶App的搜索推薦,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的可以了解一下
    2023-07-07
  • Android SDK在線更新鏡像服務(wù)器大全

    Android SDK在線更新鏡像服務(wù)器大全

    由于一些原因,Google相關(guān)很多服務(wù)都無(wú)法訪問(wèn),所以在很多時(shí)候我們SDK也無(wú)法升級(jí),當(dāng)然通過(guò)技術(shù)手段肯定可以解決,但是比較麻煩,而且下載速度也不怎么樣
    2015-10-10
  • ViewPager+RadioGroup實(shí)現(xiàn)左右滑動(dòng)卡片布局

    ViewPager+RadioGroup實(shí)現(xiàn)左右滑動(dòng)卡片布局

    這篇文章主要為大家詳細(xì)介紹了ViewPager+RadioGroup實(shí)現(xiàn)左右滑動(dòng)卡片布局,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • RecyclerView索引溢出異常的解決方法

    RecyclerView索引溢出異常的解決方法

    本篇文章主要介紹了RecyclerView索引溢出異常的解決方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • 詳解Android應(yīng)用沙盒機(jī)制

    詳解Android應(yīng)用沙盒機(jī)制

    這篇文章主要介紹了Android應(yīng)用沙盒機(jī)制的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用Android開(kāi)發(fā),感興趣的朋友可以了解下
    2021-04-04
  • Android 自定義view實(shí)現(xiàn)TopBar效果

    Android 自定義view實(shí)現(xiàn)TopBar效果

    這篇文章主要為大家詳細(xì)介紹了Android 自定義view實(shí)現(xiàn)TopBar效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-09-09

最新評(píng)論