Android 应用详情查看

应用详情查看

存储空间查看

  1. PackageStats
    该类在API26开始被遗弃

    1. 创建AIDL包android.content.pm

    2. 添加AIDL文件

      1. 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. 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;
      
    3. 添加java代码

          try {
              Method getPackageSizeInfo = mPackageManager.getClass().getMethod("getPackageSizeInfo", String.class, IPackageStatsObserver.class);
              getPackageSizeInfo.invoke(mPackageManager, packageName, new IPackageStatsObserver.Stub() {
      
                  @Override
                  public void onGetStatsCompleted(PackageStats packageStats, boolean succeeded) {
                      Log.d(TAG, Formatter.formatShortFileSize(mContext, packageStats.codeSize));
                  }
              });
          } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
              e.printStackTrace();
          }
      
    4. 注意事项:

      1. 需要添加权限GET_PACKAGE_SIZE,不需要动态申请
      2. 当尝试在高版本中调用该方法获取信息时,并不能获取到,会报错
      System.err: Caused by: java.lang.UnsupportedOperationException: Shame on you for calling the hidden API getPackageSizeInfoAsUser(). Shame!
      
      1. d
  2. StorageStatsManager

    1. Java代码
    StorageStatsManager mStorageStatsManager = ContextCompat.getSystemService(mContext,
            StorageStatsManager.class);
    try {
        final StorageStats storageStats = mStorageStatsManager.queryStatsForUid(
                StorageManager.UUID_DEFAULT, packageInfo.applicationInfo.uid);
        appInfo.setAppSize(
                Formatter.formatShortFileSize(mContext, storageStats.getAppBytes()));
    } catch (IOException e) {
        e.printStackTrace();
    }
    
    1. 注意事项:
      1. 官方文档要求在使用queryStatsForUid()申请权限,但是此处没有申请权限也能使用(测试机IQOO Neo3,但是在虚拟机中,当通过该方法请求其他UID时,会要求你申请PACKAGE_USAGE_STATS权限,但是该权限属于signature权限,请求其他UID的时候会直接拒绝该权限)

        Note: no permissions are required when calling this API for your own UID. However, requesting details for any other UID requires the android.Manifest.permission#PACKAGE_USAGE_STATS permission, which is a system-level permission that will not be granted to normal apps. Declaring that permission expresses your intention to use this API and an end user can then choose to grant this permission through the Settings application.

  3. ApplicationInfo
    如果只是想要获取应用安装的apk大小,可以使用ApplicationInfo(不需要获取权限)

    ApplicationInfo tmpInfo = context.getPackageManager().getApplicationInfo(packageName,-1);
    long size = new File(tmpInfo.sourceDir).length();
    

应用基本信息查看

  1. 应用程序主Activity
    1. 通过PackageName的getLaunchIntentForPackage

      Intent launcherIntent = packageManager.getLaunchIntentForPackage(packageName);
      ComponentName componentName = launcherIntent.getComponent();
      Log.d(TAG, componentName.getClassName());
      
    2. 通过PackageName的resolveActivity

      Intent intent = new Intent(Intent.ACTION_MAIN);
      intent.setPackage(packageInfo.packageName);
      intent.addCategory(Intent.CATEGORY_LAUNCHER);
      ResolveInfo resolveInfo = packageManager.resolveActivity(intent, PackageManager.MATCH_ALL);
      ComponentName componentName1 = new ComponentName(
              resolveInfo.activityInfo.applicationInfo.packageName,
              resolveInfo.activityInfo.name);
      
      Log.d(TAG, "onStart: " + componentName1.getClassName());
      

      参考博客1使用Intent的resolveActivity()的方法,但是实际使用中返回的结果为null,查看代码发现

      public ComponentName resolveActivity(@NonNull PackageManager pm) {
      if (mComponent != null) {
          return mComponent;
      }
      
      ResolveInfo info = pm.resolveActivity(
          this, PackageManager.MATCH_DEFAULT_ONLY);
      if (info != null) {
          return new ComponentName(
                  info.activityInfo.applicationInfo.packageName,
                  info.activityInfo.name);
      }
      
      return null;
      }
      

      获取系统获取ResolveInfo是使用的PackageManager的resolveActivity,传入的Flag默认为MATCH_DEFAULT_ONLY,根据Google的文档说明这样只能获取申明了Intent.CATEGORY_DEFAULT的Activity信息

      Resolution and querying flag: if set, only filters that support the Intent.CATEGORY_DEFAULT will be considered for matching. This is a synonym for including the CATEGORY_DEFAULT in your supplied Intent.
      
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页