1

在我的项目中,我可以通过以下代码获取所有已安装应用程序的列表:

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

但现在我想过滤列表将只显示游戏,这可能吗?

4

4 回答 4

3

在 Android 中无法将您的应用设置为“游戏类型”,因此您无法按类别过滤应用。

我认为,最好的解决方案 - 从 Google Play 解析所有游戏包(游戏名称可能因手机本地化而不同)并将该列表放入应用程序中,有时从您的服务器更新它。如果您的应用程序中始终有互联网连接,您可以将找到的应用程序列表发送到服务器,服务器将从该列表返回游戏。因此,您需要为此编写服务器代码。

于 2012-12-02T10:46:14.140 回答
1

您可以获取设备上安装的每个应用程序的包名称,并抓取 google play 商店以查看该应用程序是否属于某个游戏类别。但这有点骇人听闻:)。

于 2012-12-02T10:54:27.947 回答
1

所以首先在你的清单文件中添加这个权限

使用权限 android:name="android.permission.QUERY_ALL_PACKAGES"

如果它是真的,它将返回一个布尔值,那么你的包名来自游戏类别

   @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;
    }

}
于 2021-11-19T07:41:57.943 回答
0

不,这是不可能的。类别仅用于市场应用程序没有任何标识符来区分应用程序类别。

于 2012-12-02T10:46:36.247 回答