Android获取 应用程序大小,数据大小,缓存大小
2023-09-14 09:00:21 时间
在项目中创建,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 += "\n数据大小: " + formatFileSize(newPs.dataSize); 36 infoString += "\n缓存大小: " + 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"/>
相关文章
- Android基于DataBinding封装RecyclerView实现快速列表开发
- android onresume函数,Android界面跳转时候onDestroy和onResume的调用顺序
- android 浏览器 开发,Android 浏览器的开发实例分享
- android签名命令行,Android系统签名位置及命令
- android 验证码短信验证码,Android短信验证码倒计时验证的2种常用方式
- android 定时器实例,Android定时器和Handler用法实例分析
- iphone4装android,iPhone4可安装Android实现双系统启动.pdf
- android scaleanimation动画,Animation之ScaleAnimation(缩放动画片)「建议收藏」
- android sqlite 判断表和表中字段是否存在方法
- 【Android从零单排系列五】《Android视图控件——控件基本属性说明》
- 【Android RTMP】音频数据采集编码 ( FAAC 头文件与静态库拷贝到 AS | CMakeList.txt 配置 FAAC | AudioRecord 音频采样 PCM 格式 )
- 【Android 内存优化】Bitmap 内存缓存 ( Bitmap 内存复用 | 弱引用 | 引用队列 | 针对不同 Android 版本开发不同的 Bitmap 复用策略 | 工具类代码 )
- 【Android 安全】DEX 加密 ( Proguard 混淆 | 将混淆后的报错信息转为原始报错信息 | retrace.bat 命令执行目录 | 暴露更少信息 )
- 【Android 逆向】Android 权限 ( Android 逆向中使用的 android.permission 权限 | Android 系统中的 Linux 用户权限 )
- 【Android 逆向】应用数据目录 ( files 数据目录 | lib 应用自带 so 动态库目录 | databases sqlite3 数据库目录 | cache 缓存目录 )
- Android Glide数据更新及内存缓存、硬盘缓存清理详解手机开发
- Android端Vivaldi 3.6发布:引入Page Action功能 可自定义网页外观
- TK 教主:Android 成为漏洞王?真实漏洞排名可能另有玄机
- Android利用方向传感器实现指南针具体步骤
- 从零开始学android小示例程序
- android开发教程之清除android数据缓存示例(清除本地数据缓存)