Android获取app应用程序大小的方法
程序员文章站
2023-12-13 23:03:52
android对这种方法进行了封装,我们没有权限去调用这个方法,所以我们只能通过aidl,然后利用java的反射机制去调用系统级的方法。
下面上代码:(注释比较详细)...
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); }