Reyjohn
Reyjohn

Reputation: 2733

How to programmatically get the installed game list in Android

In my project I can get the list of all installed application by the following code:

public class ApplicationFilterActivity extends Activity implements
        OnItemClickListener {

    ListView appfilter;
    SharedPreferences preferences;
    public static String filenames = "RotateData";
PackageManager pck;
    ArrayList<Applications> results = new ArrayList<Applications>();
    ArrayList<String> gotPackagename = new ArrayList<String>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        appfilter = (ListView) findViewById(R.id.lvApp);
        preferences = getSharedPreferences(filenames, 0);
        PackageManager packageManager = this.getPackageManager();
        List<PackageInfo> applist = packageManager.getInstalledPackages(0);
        Iterator<PackageInfo> it = applist.iterator();
        while (it.hasNext()) {
            PackageInfo pk = (PackageInfo) it.next();
            results.add(new Applications(pk.applicationInfo
                    .loadIcon(packageManager), ""
                    + pk.applicationInfo.loadLabel(packageManager)));
            Log.i("AppName", "" + pk.applicationInfo.packageName);
            gotPackagename.add(pk.applicationInfo.packageName);
        }

        appfilter.setAdapter(new Customarrayadapter(this, results));
        appfilter.setOnItemClickListener(this);
    }
}

but now I want to filter that the list will show only the games , is it possible?

Upvotes: 1

Views: 2414

Answers (4)

vaidik limbachiya
vaidik limbachiya

Reputation: 111

So first add this permission in your Manifest file

uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"

it will return a boolean if it's true then your package name is from game category

   @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    binding = GameListBinding.inflate(getLayoutInflater());
    View view = binding.getRoot();
    setContentView(view);

    final PackageManager pm = getPackageManager();

    //get a list of installed apps.
    List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
    for (ApplicationInfo packageInfo : packages) {

        Boolean b = checkAppIsGame(GameList.this,packageInfo.packageName);

        if(b){
            Toast.makeText(GameList.this, "This is Game "+ packageInfo.packageName, Toast.LENGTH_SHORT).show();
        }

    }

}

public static boolean checkAppIsGame(Context context, String packageName) {
    try {
        ApplicationInfo info = context.getPackageManager().getApplicationInfo(packageName, 0);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            Log.e("MyGame", "App Packagename is game : " + packageName);
            return info.category == ApplicationInfo.CATEGORY_GAME;
        } else {
            Toast.makeText(context, "is game 2 " + packageName, Toast.LENGTH_SHORT).show();
            Log.e("Util", "Package is game 2 : " + packageName);
            return (info.flags & ApplicationInfo.FLAG_IS_GAME) == ApplicationInfo.FLAG_IS_GAME;
        }
    } catch (PackageManager.NameNotFoundException e) {
        Log.e("MyGame", "Package info Not Found For : " + packageName, e);

        return false;
    }

}

Upvotes: 1

Felix
Felix

Reputation: 89606

You could get the package name of every app installed on the device and scrape the google play store to see if that app is in one of the games categories. But that's a bit hackish :).

Upvotes: 1

darshanz
darshanz

Reputation: 412

No it is not possible. Catagories are only for using in market Applications don't have any identifier to distinguish between app catagories.

Upvotes: 0

Artem Zinnatullin
Artem Zinnatullin

Reputation: 4447

There is no way to set your app as "Game Type" in Android, so you could not filter apps by category.

I think, best solution - to parse all games packages (game name could be different because of phone localization) from Google Play and put that list in app and sometimes update it from your server. If you always got internet connection in your application you could send found apps list to server and server will return back games from that list. So, you need to write server code for that.

Upvotes: 3

Related Questions