zoukankan      html  css  js  c++  java
  • Android获取 应用程序大小,数据大小,缓存大小

          在项目中创建,android.content.pm 包名。里面创建两个aidl文件。PackageStats.aidl  和 IPackageStatsObserver.aidl。

    PackageStats.aidl

    /* //device/java/android/android/view/WindowManager.aidl
    **
    ** Copyright 2007, The Android Open Source Project
    **
    ** Licensed under the Apache License, Version 2.0 (the "License"); 
    ** you may not use this file except in compliance with the License. 
    ** You may obtain a copy of the License at 
    **
    **     http://www.apache.org/licenses/LICENSE-2.0 
    **
    ** Unless required by applicable law or agreed to in writing, software 
    ** distributed under the License is distributed on an "AS IS" BASIS, 
    ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
    ** See the License for the specific language governing permissions and 
    ** limitations under the License.
    */
    
    package android.content.pm;
    
    parcelable PackageStats;

    IPackageStatsObserver.aidl

    /*
    **
    ** Copyright 2007, The Android Open Source Project
    **
    ** Licensed under the Apache License, Version 2.0 (the "License");
    ** you may not use this file except in compliance with the License.
    ** You may obtain a copy of the License at
    **
    **     http://www.apache.org/licenses/LICENSE-2.0
    **
    ** Unless required by applicable law or agreed to in writing, software
    ** distributed under the License is distributed on an "AS IS" BASIS,
    ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    ** See the License for the specific language governing permissions and
    ** limitations under the License.
    */
    
    package android.content.pm;
    
    import android.content.pm.PackageStats;
    /**
     * API for package data change related callbacks from the Package Manager.
     * Some usage scenarios include deletion of cache directory, generate
     * statistics related to code, data, cache usage(TODO)
     * {@hide}
     */
    oneway interface IPackageStatsObserver {
        void onGetStatsCompleted(in PackageStats pStats, boolean succeeded);
    }
     1  import android.app.Activity;
     2 import android.content.pm.IPackageStatsObserver;
     3 import android.content.pm.PackageManager;
     4 import android.content.pm.PackageStats;
     5 import android.os.Bundle;
     6 import android.os.Handler;
     7 import android.os.Message;
     8 import android.widget.TextView;
     9 
    10 public class MainActivity extends Activity {
    11     
    12     private TextView tv;
    13     private static final String ATTR_PACKAGE_STATS = "PackageStats";
    14 
    15     @Override
    16     public void onCreate(Bundle savedInstanceState) {
    17         super.onCreate(savedInstanceState);
    18         
    19         tv = new TextView(this);
    20         
    21         setContentView(tv);
    22         
    23         getpkginfo("com.xj.notebook");
    24     }
    25 
    26     private Handler mHandler = new Handler() {
    27         public void handleMessage(Message msg) {
    28             switch (msg.what) {
    29             case 1:
    30                 String infoString = "";
    31                 PackageStats newPs = msg.getData().getParcelable(
    32                         ATTR_PACKAGE_STATS);
    33                 if (newPs != null) {
    34                     infoString += "应用程序大小: " + formatFileSize(newPs.codeSize);
    35                     infoString += "
    数据大小: " + formatFileSize(newPs.dataSize);
    36                     infoString += "
    缓存大小: " + formatFileSize(newPs.cacheSize);
    37                 }
    38                 tv.setText(infoString);
    39                 break;
    40             default:
    41                 break;
    42             }
    43         }
    44     };
    45 
    46     public void getpkginfo(String pkg) {
    47         PackageManager pm = getPackageManager();
    48         try {
    49             Method getPackageSizeInfo = pm.getClass().getMethod(
    50                     "getPackageSizeInfo", String.class,
    51                     IPackageStatsObserver.class);
    52             getPackageSizeInfo.invoke(pm, pkg, new PkgSizeObserver());
    53         } catch (Exception e) {
    54         }
    55     }
    56 
    57     class PkgSizeObserver extends IPackageStatsObserver.Stub {
    58         public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) {
    59             Message msg = mHandler.obtainMessage(1);
    60             Bundle data = new Bundle();
    61             data.putParcelable(ATTR_PACKAGE_STATS, pStats);
    62             msg.setData(data);
    63             mHandler.sendMessage(msg);
    65         }
    66     }
    67 
    68     /**
    69      * 获取文件大小
    70      */
    71     public static String formatFileSize(long length) {
    72         String result = null;
    73         int sub_string = 0;
    74         if (length >= 1073741824) {
    75             sub_string = String.valueOf((float) length / 1073741824).indexOf(
    76                     ".");
    77             result = ((float) length / 1073741824 + "000").substring(0,
    78                     sub_string + 3) + "GB";
    79         } else if (length >= 1048576) {
    80             sub_string = String.valueOf((float) length / 1048576).indexOf(".");
    81             result = ((float) length / 1048576 + "000").substring(0,
    82                     sub_string + 3) + "MB";
    83         } else if (length >= 1024) {
    84             sub_string = String.valueOf((float) length / 1024).indexOf(".");
    85             result = ((float) length / 1024 + "000").substring(0,
    86                     sub_string + 3) + "KB";
    87         } else if (length < 1024)
    88             result = Long.toString(length) + "B";
    89         return result;
    90     }
    91 }
    <uses-permission android:name="android.permission.GET_PACKAGE_SIZE"/> 
  • 相关阅读:
    Delphi中 StrToIntDef函数的用法:
    Delphi判断文件夹(目录)是否存在,不存在就创建一个,可以一次创建多级目录
    Delphi中Format的字符串格式化使用说明
    linux下可变参数打印
    c++11 thread
    QMessageBox 使用
    Ubuntu虚拟机配置nfs
    test
    Qt 布局时组建不贴边
    QPainter 居中写字
  • 原文地址:https://www.cnblogs.com/androidsj/p/5112097.html
Copyright © 2011-2022 走看看