由于我知道这一方面,我的大部分问题都已经通过搜索得到了回答。但这似乎是一个特殊的问题。
我是Google Android开发的新手,所以我想通过实践来学习。我想创建一个应用程序,扫描条形码,并显示一个可能的产品列表。我正在使用ZXing (从这里获取!;))来扫描barcdes。效果很好。我正在用扫描的条形码查询google购物api,并解析rss提要,因为有一个结果(但在大多数情况下是^^)。
这一切都很好用。但在我扫描应用程序返回到我的活动后,这需要四秒钟的时间。这看起来很长,不是吗?所以我想把扫描部分移到一个线程中,在查询来自api的结果时,应该有一个用户的ProgressDialog,这样他就知道那里发生了什么。
到目前为止,我的代码如下:
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
switch (requestCode) {
case IntentIntegrator.REQUEST_CODE:
if (resultCode == Activity.RESULT_OK) {
IntentResult intentResult =
IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (intentResult != null) {
String scannedCode = intentResult.getContents();
String format = intentResult.getFormatName();
//Gets information to the scanned product by the
//Google-Shopping-API in form of a rss feed.
AndroidFeedParser afp = new AndroidFeedParser("https://www.googleapis.com/shopping/search/v1/public/products?key=" +
AndroidFeedParser.API_KEY + "&country=DE&q=" + scannedCode + "&alt=" + AndroidFeedParser.FEED_CODE);
//Parses the given rss feed and gives a FeedItem-List
List<FeedItem> item = afp.parse();
Toast toast = null;
if(item.size() < 1)
//products is NOT listed within the Google-Shopping-API
toast = Toast.makeText(this, "Dieses Produkt ist nicht in der Datenbank.", Toast.LENGTH_LONG);
else
//product is listed within the Google-Shopping-API
toast = Toast.makeText(this, item.get(1).getPrice().toString(), Toast.LENGTH_LONG);
toast.show();
Log.d("SEARCH_EAN", "OK, EAN: " + scannedCode + ", FORMAT: " + format);
} else {
Log.e("SEARCH_EAN", "IntentResult je NULL!");
}
} else if (resultCode == Activity.RESULT_CANCELED) {
Log.e("SEARCH_EAN", "CANCEL");
}
} }
我告诉过你,这很管用。但是这需要花费很多时间,我猜对api的查询花费了这么多时间。我希望你能理解我的问题,并能帮助我!我确实知道,必须实现一个接口Runnable和方法run(),但我不知道我必须分离代码的哪一部分才能存档上面所希望的。请帮帮我!
致以最好的问候,巴斯蒂
(对不起,我的英语不好,我来自德国)
发布于 2012-01-30 23:50:23
您应该在创建AndroidFeedParser时从行启动一个新的异步任务...但也许你的应用已经完成了?;)
https://stackoverflow.com/questions/7231205
复制相似问题