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

huangapple go评论56阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年2月27日 09:49:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75576173.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定