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.