getApplicationInfo(String,int) deprecated, What is the alternative?
Asked Answered
L

2

10

I have upgraded targetSdkVersion and compileSdkVersion to 33.

Now getting warning getApplicationInfo(String,int) is deprecated.

It is suggested to use use getApplicationInfo(java.lang.String, android.content.pm.PackageManager.ApplicationInfoFlags) instead. Anyone can help me to use the updated method.

Example:

@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();
        }
    }
}

So, I did this:

    @SuppressLint("HardwareIds")
    public void initDbx(){
        try {
//            ApplicationInfo app = activity.getPackageManager().getApplicationInfo(activity.getPackageName(), PackageManager.GET_META_DATA);

            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);
            }
            
            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();
        }
    }
}

Get the error (Cannot resolve symbol 'app')

            dbx.game_id      = String.valueOf(app.metaData.getInt("main_id"));

I created local variable 'app':

            PackageItemInfo app;
            dbx.game_id      = String.valueOf(app.metaData.getInt("main_id"));

Get the error (Variable 'app' might not have been initialized)

I initialize the variable; I tried both:

            PackageItemInfo app = null;
            dbx.game_id      = String.valueOf(app.metaData.getInt("main_id"));

and:

            PackageItemInfo app = new PackageItemInfo();
            dbx.game_id      = String.valueOf(app.metaData.getInt("main_id"));

But I kept getting the same warning, and I couldn't even run the app in the emulator this time.

Levitus answered 27/2, 2023 at 1:49 Comment(0)
S
9

getApplicationInfo(String packageName,int flags) have been deprecated in Android Api 33.
We need to use 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);
  }

You can pass either zero or combinations of other component info flags in ApplicationInfoFlags. More details in ApplicationInfoFlags

Edited: Completed initDbx() method

 public void initDbx(){
        try {
            ApplicationInfo app;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) {
                app = activity.getPackageManager().getApplicationInfo(activity.getPackageName(), PackageManager.ApplicationInfoFlags.of(0));
            }else{
                 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();
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }
Sea answered 27/2, 2023 at 5:14 Comment(8)
Thanks for the answer @Nitish, I've edited my question, can you check it out?Levitus
@Levitus , make ApplicationInfo app a global variable , current it's local inside if-elseSea
How do I do that Did you mean to create field 'app' in UtilsManager Like this: private PackageItemInfo app; Because this prevented the app from launching in the emulatorLevitus
Why would that prevent app from launching? . Anyways you can just make it outside the if else statement so that is accessible out of the conditions. Have edited my answerSea
I was making it after the if-else its launching now but I'm still getting the deprecation warning.Levitus
It doesn't show at my system, can you please share more details on this,Sea
like what? ask me. This java file is about 200 lines long, all I know is that I keep getting this warning as long as I don't completely get rid of this one; app = activity.getPackageManager().getApplicationInfo(activity.getPackageName(), PackageManager.GET_META_DATA);Levitus
handle-is-deprecated-deprecated-in-java this could be most probable reason why warning is still showingSea
K
1

I've faced problems when tried to use the new api, the returned metaData was null.

Before deprecation I used:

val appInfo = activity.getPackageManager().getApplicationInfo(activity.getPackageName(), PackageManager.GET_META_DATA);
val metaData : Bundle = app.metaData //Returned null!

But when try to use the new api with PackageManager.ApplicationInfoFlags.of(0) the metaData was null! So instead I had to use it like this:

packageManager.getApplicationInfo(packageName, PackageManager.ApplicationInfoFlags.of(GET_META_DATA.toLong()))

Then returned metaData was ok.

Kruller answered 27/11, 2023 at 13:38 Comment(1)
You are right! the PackageManager.ApplicationInfoFlags.of(0) not working if we want meta data bundle, the top answer spend my time with the problem of null meta data @@Pinole

© 2022 - 2024 — McMap. All rights reserved.