Android--获取App应用程序的大小
Android对这种方法进行了封装,我们没有权限去调用这个方法,所以我们只能通过AIDL,然后利用Java的反射机制去调用系统级的方法。
下面上代码:(注释比较详细)
/**
* 作用:-----获取包的大小-----
* @param context 上下文
* @param pkgName app的包名
* @param appInfo 实体类,用于存放App的某些信息
*/
public static void getPkgSize(final Context context, String pkgName, final PhoneAppInfo appInfo) {
// getPackageSizeInfo是PackageManager中的一个private方法,所以需要通过反射的机制来调用
Method method;
try {
method = PackageManager.class.getMethod("getPackageSizeInfo",
new Class[]{String.class, IPackageStatsObserver.class});
// 调用 getPackageSizeInfo 方法,需要两个参数:1、需要检测的应用包名;2、回调
method.invoke(context.getPackageManager(), pkgName,
new IPackageStatsObserver.Stub() {
@Override
public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws RemoteException {
if (succeeded && pStats != null) {
synchronized (PhoneAppInfo.class) {
appInfo.setCatchSize(pStats.cacheSize);//缓存大小
appInfo.setDataSize(pStats.dataSize); //数据大小
appInfo.setCodeSize(pStats.codeSize); //应用大小
appInfo.setAppSize(pStats.cacheSize + pStats.codeSize + pStats.dataSize);//应用的总大小
Log.d("asdasdxx",appInfo.getAppSize()+"");
}
}
}
});
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
e.printStackTrace();
}
}
下面是两个AIDL文件的代码。。。
步骤(Android Studio):1、在main文件夹下,建立一个aidl文件夹的文件夹
2、建立一个包,包名为android.content.pm
3、结构图
*******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;
****************IPackageStatusObserver.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);
}
Android--获取App应用程序的大小的更多相关文章
- android获取APP 包名和activity
1.连接设备并启动被测试app应用 2.打开cmd窗口 3.windows获取包名和activity adb shell dumpsys activity |find "mFocuse ...
- Android获取App版本号和版本名
1 //获取版本名 public static String getVersionName(Context context) { return getPackageInfo(context).vers ...
- Android获取屏幕大小和设置无标题栏
android获取屏幕大小非常常用,例如写个程序,如果要做成通用性很强的程序,适用屏幕很强,一般布局的时候都是根据屏幕的长宽来定义的,所以我把这个总结一下,方便日后忘记的时候查阅.还有就是有时候写程序 ...
- Android中App可分配内存的大小
现在真实测试结果: 1,为了搞清楚每个应用程序在Android系统中最多可分配多少内存空间,我们使用了真机进行测试,测试机型为魅族MX4 Pro,3G内存. 测试方法是直接申请一块较大的内存空间,看应 ...
- Android中App可分配内存的大小(转)
转自:http://blog.csdn.net/u011506413/article/details/50965435 现在真实测试结果: 1,为了搞清楚每个应用程序在Android系统中最多可分配多 ...
- wemall app商城源码Android 获取XML网络数据并绑定到ListView
wemall-mobile是基于WeMall的android app商城,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可定制修改.本文分享Android 获取XML网络数据并绑定到Li ...
- Android获取程序路径 (/data/data/appname)
Android获取文件夹路径 /data/data/ http://www.2cto.com/kf/201301/186614.html String printTxtPath = getApplic ...
- Android获取屏幕大小
本来想着如下方法就能得到了 Display display = getWindowManager().getDefaultDisplay(); Log.i("view", &quo ...
- 微信小程序开发之如何哪获取微信小程序的APP ID
微信小程序的开发工具,在新建项目的时候,默认提示填写APP ID,如果不填写AppID 也是可以本地测试和开发的,但是无法通过手机调试,只能在开发工具里查看 如果需要真机调试微信小程序,需要安装微信6 ...
随机推荐
- mysql数据导入
1.windows解压 2.修改文件名,例如a.txt 3.rz 导入到 linux \data\pcode sudo su -cd /data/pcode/rm -rf *.txt 4.合并到一个文 ...
- 华为OJ平台——超长正整数相加
题目描述: 请设计一个算法完成两个超长正整数的加法. 输入 输入两个字符串数字 输出 输出相加后的结果,string型 样例输入 99999999999999999999999999999999999 ...
- WinDbg调试CPU占用高的问题 试验+实战 《第七篇》
一.High CPU试验 1.示例代码 static void Main(string[] args) { Console.Clear(); Console.WriteLine("到命令行下 ...
- css选择器分类
css选择器大致可以分为10大类: 1.元素选择器 如:p{} 2.类选择器 如:.xx{} 3.ID选择器 如:#xx{} 4.关联选择器 如:p a{} 5.子元素选择器 如:p>a{} 6 ...
- ios开发经典语录锦集
原文链接: iPhone开发经典语录集锦 前言:iPhone是个极具艺术性的平台,相信大家在开发过程中一定有很多感触,希望能写出来一起交流,所以开了这个帖子,以后还会维护. 如果大家和我一样有感触的话 ...
- sql基础查询语句
数据库文件百度云地址:www.pan.baidu.com 脚步:下载博客园文件:select_learn.rar 1.TOP限制返回行数[percent] * from book_info --显示前 ...
- global.autoindex dede:global.itemindex 获取子栏目自动排序序号
{dede:channel row='6' typeid=7 } [field:global.autoindex runphp='yes'] ...
- 快速搭建 Node.js 开发环境以及加速 npm
如何快速搭建 node 开发环境 npm 超慢 github 无法打开的问题 于是我觉得应该写一篇文章解答所有这些起步问题,让新同学也能顺顺利利入门. 快速搭建 Node.js 开发环境 如果你想长期 ...
- 兰勃特投影C#实现
兰勃特投影是等面积投影. static double PI = 3.1415926; //------------------------------------------------------- ...
- Html.ActionLink
一 Html.ActionLink("linkText","actionName") 该重载的第一个参数是该链接要显示的文字,第二个参数是对应的控制器的方法, ...