在自己的线程中扫描条形码(ZXing)并显示 ProgessDialog
因为我知道这一面,我的大部分问题都已通过搜索得到了解答。但这似乎是一件特别的事。
我是 Google Android 开发的新手,所以我想边做边学。我想创建一个应用程序,可以扫描条形码并显示可能的产品列表。我正在使用 ZXing(从这里得到这个!;))来扫描条码。作品完美。我正在使用扫描的条形码查询谷歌购物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() 方法,但我不知道我必须分离代码的哪一部分来归档上述愿望。请帮我!
此致, Basti
(请原谅我的英语不好,我来自德国)
since I know this side most of my questions have been answered be using the search. But this seems to be a special one.
I am new to Google Android development, so I want to learn by doing. I want to create an app, that scans barcodes and displays a list of possible products. I am using ZXing (got this from here! ;)) to scan the barcdes. Works perfect. I'm quering the google shopping api with the scanned barcode and get parse the rss feed, since there is a result (but in most cases there is ^^).
This all works great. But it takes up to four seconds after I scan the app returns to my activitiy. This seems very long to, isn't it? So I thought to move the scanning part into a single thread and while quering the result from the api there should be a ProgressDialog for the user, that he knows whats going on there.
Thats, my Code so far:
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");
}
} }
I told ya, this works awesome. But this takes to much time, I guess the query to the api costs that much time. I hope you understand my question and you can help me! I do know, that there must be an Interface Runnable implemented and the method run(), but I do not know what part of code I do have to separate to archive that wish above. Please help me!
Best regards,
Basti
(Excuse me for my bad english, I'm from Germany)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您创建 AndroidFeedParser 时,您应该从行开始一个新的异步任务...但也许您的应用程序已经完成了? ;)
You should start a new asynchronous task from line when you create AndroidFeedParser... but maybe your app is already done? ;)