Android obtains the APP name according to the package name

Android obtains the APP name according to the package name


When I was working today, I encountered a problem. Goole paly music needs to get the name of the APP, but the method in the source code gets a null value. According to the information on the Internet, record the method of getting the name of the APP.
Create a new Activity with a button and a TextView. This example mainly obtains the name of the calculator based on the package name of the mobile calculator. If you don't know the package name of the program, you can use adb to get it, first open the program, and then enter the command:

adb shell dumpsys window w | findstr name | findstr \/

The code for the button click event is as follows:

button1.setOnClickListener((view)->{
    
    
        String str = "com.android.calculator2";
        PackageInfo info = null;
        PackageManager pm = getPackageManager();
        try {
    
    
            info = pm.getPackageInfo(str,PackageManager.GET_ACTIVITIES);
        } catch (PackageManager.NameNotFoundException e) {
    
    
            e.printStackTrace();
        }
        text.setText(info.applicationInfo.loadLabel(pm).toString());
});

Guess you like

Origin blog.csdn.net/qq_36224961/article/details/100145502