Scanning Barcodes (ZXing) in a own Thread and displaying a ProgessDialog

风格不统一 提交于 2019-12-08 06:32:01

问题


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)


回答1:


You should start a new asynchronous task from line when you create AndroidFeedParser... but maybe your app is already done? ;)



来源:https://stackoverflow.com/questions/7231205/scanning-barcodes-zxing-in-a-own-thread-and-displaying-a-progessdialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!