5

我正在一个应用程序中工作,我正在使用以下代码获取安装在我的设备上的所有应用程序:

final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
 mainIntent.addCategory("com.myapp.MY_CATEGORY");
 final List<ResolveInfo> pkgAppsList =getPackageManager().queryIntentActivities( mainIntent, 0);

现在我想根据它们的类别对应用程序进行不同的分类。请建议我。

4

2 回答 2

2

To know the category of an application you need to get the data from google play. you can check android-market-api. it is a third party api. according to their info

  • You can browse market with any carrier or locale you want.
  • Search for apps using keywords or package name.
  • Retrieve an app info using an app ID.
  • Retrieve comments using an app ID.
  • Get PNG screenshots and icon

So you better check if you can parse the category info using this api.

于 2013-05-24T13:55:12.357 回答
0

我实现了一个 AsyncTask 来收集一些应用程序的类别,使用这些库:

  • android-market-api-0.6

  • com.google.protobuf 2.4.1

您可以在此链接上找到它们:

https://code.google.com/archive/p/android-market-api/downloads

http://mvnrepository.com/artifact/com.google.protobuf/protobuf-java

这是 doInBackground() 方法中的代码:

        final ArrayList<MarketApplication> results = new ArrayList<>();
        AccountManager am = AccountManager.get(MainActivity.this.getBaseContext());
        Account[] accounts = am.getAccountsByType("com.google");
        if (accounts.length > 0) {
            try {
                AccountManagerFuture<Bundle> accountManagerFuture =
                        am.getAuthToken(accounts[0], "android", null, MainActivity.this, null,
                                null);
                Bundle authTokenBundle = accountManagerFuture.getResult();
                String authToken =
                        authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN).toString();

                MarketSession session = new MarketSession();
                session.setAuthSubToken(authToken);

                Market.AppsRequest appsRequest = Market.AppsRequest.newBuilder()
                        .setQuery(params[0])
                        .setStartIndex(0).setEntriesCount(10)
                        .setWithExtendedInfo(true)
                        .build();


                session.append(appsRequest, new MarketSession.Callback<Market.AppsResponse>() {
                    public void onResult(Market.ResponseContext context, Market.AppsResponse
                            response) {
                        for (int i = 0; i < response.getEntriesCount(); i++) {
                            MarketApplication marketApplication = new MarketApplication();
                            Market.App app = response.getApp(i);
                            marketApplication.setName(app.getTitle());
                            Market.App.ExtendedInfo extendedInfo = app.getExtendedInfo();
                            marketApplication.setCategory(extendedInfo.getCategory());
                            results.add(marketApplication);
                        }
                    }
                });
                session.flush();
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
        return results;

从 ExtendedInfo 获得的类别信息和应用程序名称被添加到自定义类MarketApplication中。params[0]是一个查询字符串,例如感兴趣的应用名称。有一个 wiki 页面可以帮助开发人员进行特定查询:

https://code.google.com/archive/p/android-market-api/wikis/HowToSearchApps.wiki

请注意,此服务需要在 Android 清单中添加这些权限:

 <uses-permission android:name="android.permission.GET_ACCOUNTS"></uses-permission>
<uses-permission android:name="android.permission.USE_CREDENTIALS"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
于 2016-02-26T23:38:22.093 回答