我实现了一个 AsyncTask 来收集一些应用程序的类别,使用这些库:
您可以在此链接上找到它们:
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>