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应用程序的大小的更多相关文章

  1. android获取APP 包名和activity

    1.连接设备并启动被测试app应用 2.打开cmd窗口 3.windows获取包名和activity    adb shell dumpsys activity |find "mFocuse ...

  2. Android获取App版本号和版本名

    1 //获取版本名 public static String getVersionName(Context context) { return getPackageInfo(context).vers ...

  3. Android获取屏幕大小和设置无标题栏

    android获取屏幕大小非常常用,例如写个程序,如果要做成通用性很强的程序,适用屏幕很强,一般布局的时候都是根据屏幕的长宽来定义的,所以我把这个总结一下,方便日后忘记的时候查阅.还有就是有时候写程序 ...

  4. Android中App可分配内存的大小

    现在真实测试结果: 1,为了搞清楚每个应用程序在Android系统中最多可分配多少内存空间,我们使用了真机进行测试,测试机型为魅族MX4 Pro,3G内存. 测试方法是直接申请一块较大的内存空间,看应 ...

  5. Android中App可分配内存的大小(转)

    转自:http://blog.csdn.net/u011506413/article/details/50965435 现在真实测试结果: 1,为了搞清楚每个应用程序在Android系统中最多可分配多 ...

  6. wemall app商城源码Android 获取XML网络数据并绑定到ListView

    wemall-mobile是基于WeMall的android app商城,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可定制修改.本文分享Android 获取XML网络数据并绑定到Li ...

  7. Android获取程序路径 (/data/data/appname)

    Android获取文件夹路径 /data/data/ http://www.2cto.com/kf/201301/186614.html String printTxtPath = getApplic ...

  8. Android获取屏幕大小

    本来想着如下方法就能得到了 Display display = getWindowManager().getDefaultDisplay(); Log.i("view", &quo ...

  9. 微信小程序开发之如何哪获取微信小程序的APP ID

    微信小程序的开发工具,在新建项目的时候,默认提示填写APP ID,如果不填写AppID 也是可以本地测试和开发的,但是无法通过手机调试,只能在开发工具里查看 如果需要真机调试微信小程序,需要安装微信6 ...

随机推荐

  1. 【练习】sqlnet.ora

    在SQLNET.ora文件中设置以下参数可以实现IP访问限制: $ pwd/u01/app/oracle/product/10.2.0/db_1/network/admin$ vi sqlnet.or ...

  2. 【MVC】ASP.NET MVC中实现多个按钮提交的几种方法

    有时候会遇到这种情况:在一个表单上需要多个按钮来完成不同的功能,比如一个简单的审批功能. 如果是用webform那不需要讨论,但asp.net mvc中一个表单只能提交到一个Action处理,相对比较 ...

  3. Redis+php-resque实现消息队列

      服务器硬件配置 Dell PowerEdge R310英特尔单路机架式服务器 Intel Xeon Processor X3430 2.4GHz, 8MB Cache 8GB内存(2 x 4GB) ...

  4. Git入门详解

    查看分支:git branch 创建分支:git branch <name> 切换分支:git checkout <name> 创建+切换分支:git checkout -b ...

  5. JSF Action 与ActionListener的区别

    JSF Action 与ActionListener的区别 标签: 杂谈    事件  检验  参数  事件产生  页面跳转  Action  有 无参数,不传入当前控件,有返回值    当铵钮被单击 ...

  6. android中关闭软键盘

    /**隐藏软键盘**/ View view = getWindow().peekDecorView(); if (view != null) { InputMethodManager inputman ...

  7. LTE参考资料

    NETMANIAS TECHNICAL DOCUMENTS: http://www.netmanias.com/en/?m=board&id=techdocs

  8. HTML 表单总结http://images2015.cnblogs.com/blog/1001203/201607/1001203-20160730200559841-2144892373.png

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  9. WinRt BottomAppBar

    BottomAppBarDemo.xaml <Page.BottomAppBar> <AppBar> <StackPanel Orientation="Hori ...

  10. memcached for windows 修改端口和最大内存

    解压后只要在命令窗口中输入下面命令c:\memcached\memcached.exe -d install  就可以把memcached安装为windows服务了. 启动该服务后,memcached ...