android getApplicationInfo(String,int)已弃用,有什么替代方案?

5us2dqdw  于 2023-02-27  发布在  Android
关注(0)|答案(1)|浏览(599)

我已经将targetSdkVersion和compileSdkVersion升级到33。
现在获取警告getApplicationInfo(String,int)已弃用。
建议改用use getApplicationInfo(java.lang.String,android.content.pm.PackageManager.ApplicationInfoFlags),大家可以帮我使用更新后的方法。
示例:

@SuppressLint("HardwareIds")
    public void initDbx(){
        try {
            ApplicationInfo app = activity.getPackageManager().getApplicationInfo(activity.getPackageName(), PackageManager.GET_META_DATA);
            dbx.game_id      = String.valueOf(app.metaData.getInt("main_id"));
            dbx.game_name    = activity.getResources().getString(R.string.app_name);
            dbx.package_name = activity.getApplication().getPackageName();
            dbx.user_id      = Settings.Secure.getString(activity.getContentResolver(), Settings.Secure.ANDROID_ID);
            dbx.check_net    = isConnectionAvailable();
            //dbx.getUserInfos(activity);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }
}
8gsdolmq

8gsdolmq1#

getApplicationInfo(String packageName,int flags)已在Android API 33中弃用。
我们需要使用getApplicationInfo(java.lang.String,android.content.pm.PackageManager.ApplicationInfoFlags)

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) {
      ApplicationInfo app = activity.getPackageManager().getApplicationInfo(activity.getPackageName(), PackageManager.ApplicationInfoFlags.of(0));
  }else{
      ApplicationInfo app = activity.getPackageManager().getApplicationInfo(activity.getPackageName(), PackageManager.GET_META_DATA);
  }

您可以在ApplicationInfoFlags中传递零或其他组件信息标志的组合。

相关问题