U
U
uchiha3222021-08-09 14:59:56
Java
uchiha322, 2021-08-09 14:59:56

How to call this class correctly?

Hello, I'm new to android development (
I was shown this code but I don't know how to implement it correctly in the application, please help!)

public class GetApps {
    public static List<Drawable> getApps(Activity activity) {
        List<Drawable> result = new ArrayList<>();
        PackageManager packageManager = activity.getPackageManager();
        List<ApplicationInfo> listOfAppInfo = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);

        for (ApplicationInfo applicationInfo : listOfAppInfo) {
            // this is our app
            if (applicationInfo.packageName.equals(activity.getPackageName())) continue;

            // this is system app
            if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) continue;

            result.add(packageManager.getApplicationIcon(applicationInfo));
        }

        return result;
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2021-08-09
@uchiha322

class GetApps {
    public static List<Drawable> getApps(Activity activity) {
        List<Drawable> result = new ArrayList<>();
        PackageManager packageManager = activity.getPackageManager();
        List<ApplicationInfo> listOfAppInfo = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);

        for (ApplicationInfo applicationInfo : listOfAppInfo) {
            // this is our app
            if (applicationInfo.packageName.equals(activity.getPackageName())) continue;

            // this is system app
            if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) continue;

            result.add(packageManager.getApplicationIcon(applicationInfo));
        }

        return result;
    }

}

public class Main extends Activity {
    public void main() {
        //это лишь пример вызова, вызов в твоём проекте осуществляться может в любом месте, в activity!
        List<Drawable> result = GetApps.getApps(this);
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question