英文:
getApplicationInfo(String,int) deprecated, What is the alternative?
问题
I have upgraded targetSdkVersion and compileSdkVersion to 33.
Now getting warning getApplicationInfo(String,int) is deprecated.
It is suggested to 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;
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();
//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);
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();
//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.
英文:
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.
答案1
得分: 8
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
中传递零或其他组件信息标志的组合。更多细节请参考 ApplicationInfoFlags
已编辑: 完成了 initDbx()
方法
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();
}
}
英文:
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();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论