Android方法同步调用

时间:2017-05-12 10:29:09

标签: android methods

我正在使用此功能安装应用程序的大小:

Method getPackageSizeInfo = pm.getClass().getMethod("getPackageSizeInfo", String.class, IPackageStatsObserver.class);
getPackageSizeInfo.invoke(pm, applicationInfo.packageName, new IPackageStatsObserver.Stub() {
  @Override
  public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws RemoteException {
    //my code here
  }
});

由于我必须在list中显示所有信息,如何在为所有其他应用程序执行相同的工作之前等待此方法完成?

有没有办法同步调用此Method.invoke

修改

这是我的完整代码:

    List<ApplicationInfo> applicationInfos;
    final PackageManager pm = ctx.getPackageManager();
    applicationInfos = pm.getInstalledApplications(0);
    Context ctx = getContext();

    for (ApplicationInfo ai :
            applicationInfos) {
        if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
            allApps.add(new MyApp(pm, ai, ctx));
        }
    }

这是构造函数(obv我可以将大小检索部分分开并将其放在构造函数之外。

public MyApp(PackageManager pm, ApplicationInfo applicationInfo, Context ctx){

    MyApp.this.packageName = applicationInfo.packageName;
    MyApp.this.label = applicationInfo.loadLabel(pm).toString();
    MyApp.this.icon = applicationInfo.loadIcon(pm);


    try {
        long installed = ctx
                .getPackageManager()
                .getPackageInfo(applicationInfo.packageName, 0)
                .firstInstallTime;

        MyApp.this.installDate = new Date(installed);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        MyApp.this.installDate = null;
    }

    try {
        Method getPackageSizeInfo = pm.getClass().getMethod(
                "getPackageSizeInfo", String.class, IPackageStatsObserver.class);
        getPackageSizeInfo.invoke(pm, applicationInfo.packageName,
                new IPackageStatsObserver.Stub() {
                    @Override
                    public void onGetStatsCompleted(PackageStats pStats, boolean succeeded)
                            throws RemoteException {

                        int tempSize = Math.round((pStats.codeSize + pStats.cacheSize + pStats.dataSize) / 10000);
                        double finalSize = tempSize/100;
                        MyApp.this.size = finalSize;
                    }
                });

    } catch (Exception ignored) {

    }

}

0 个答案:

没有答案