【发布时间】:2011-11-06 01:39:13
【问题描述】:
因为我知道这一面,所以我的大部分问题都通过搜索得到了解答。但这似乎是一个特殊的。
我是 Google Android 开发的新手,所以我想边做边学。我想创建一个应用程序,它可以扫描条形码并显示可能的产品列表。我正在使用 ZXing(从这里得到这个!;))来扫描 barcdes。完美运行。我正在使用扫描的条形码查询谷歌购物 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(),但我不知道我必须分离代码的哪一部分来归档上面的愿望。请帮帮我!
最好的问候, 巴斯蒂
(对不起,我的英语不好,我来自德国)
【问题讨论】:
标签: android multithreading barcode zxing