2012-10-04 78 views
0

我有一個應用程序,它掃描qrcode以使用ZXing庫獲取客戶端數據。 qrcode保存經度和緯度數據。掃描發生時,我可以從掃描結果中獲取信息。這是在ZXing掃描的onActivityResult中。在onActivityResult裏面,我啓動一個服務來查找用戶位置,然後將服務的結果與qrcode中的結果進行比較。然後將掃描結果以及服務中的lon和lat值存儲在手機上的sqlite數據庫中,這稱爲事務。如果數據庫爲空,則該事務將存儲到數據庫中。如果數據庫中已經存在事務,那麼該服務將運行兩次,即使我已經調用了stopService。任何可以告訴我爲什麼。Android服務在ZXing的onActivityResults中的stopService後運行

我已註銷該服務被銷燬,是否有任何理由服務可以運行後,它已被銷燬?謝謝。

public void onActivityResult(int requestCode, int resultCode, Intent intent) { 
     Log.e(TAG, "in onActivityResult from ZXing"); 
     if (requestCode == 0) { 
      if (resultCode == RESULT_OK) { 
       Log.e(TAG, "result ok"); 
       /////////////////////////////// 
       tagScanTime = new DateTime(); 
       thirtySecsAgo = tagScanTime.minus(30000); 
       DateTimeFormatter df = DateTimeFormat.forPattern("dd/MMM/yy h:mmaa"); 
       String formattedScanTime = df.print(tagScanTime); 
       Log.e(TAG, "formatted tag scan time = " + formattedScanTime); 
       String formattedthirtysecsAgoTime = df.print(thirtySecsAgo); 
       Log.e(TAG, "formatted thity secs ago time = " + formattedthirtysecsAgoTime); 


       contents = intent.getStringExtra("SCAN_RESULT"); 
       Toast.makeText(this, "scanner has found " + contents, 
         Toast.LENGTH_LONG).show(); 



       locationChangereceiver = new BroadcastReceiver() { 

         @Override 
         public void onReceive(Context context, Intent intent) { 

          LocationChangeIntent myIntent = (LocationChangeIntent) intent; 
          lon = myIntent.getLongitude(); 
          lat = myIntent.getLaltitude(); 

          Log.e(TAG, "values from service =========="+lon+" "+ lat); 
          // stop the service. 
          stopService(new Intent(context, LocationService.class)); 
          Log.e(TAG, "just called stopService in nfcscsnActivity"); 
          String[] splitPayload = contents.split("@"); 


          tagType = splitPayload[0]; 
          tagCompany = splitPayload[1]; 
          tagPerson = splitPayload[2]; 
          tagUserName = splitPayload[3]; 
          ////////////////////////////////following values currently not stored to DB 
          tagLongitude = splitPayload[4]; 
          tagLatitude = splitPayload[5]; 

          Log.e(TAG, "about to compare lon/lat of tag to lon/lat of service"); 
          if(tagLongitude.toString().trim().equalsIgnoreCase(String.valueOf(lon)) 
            && tagLatitude.toString().trim().equalsIgnoreCase(String.valueOf(lat))){ 


           showToast("carer not in exact position"); 
           Log.e(TAG, "carer not in exact position"); 
          }else{ 

           showToast("carer is in exact position"); 
           Log.e(TAG, "carer is in exact position"); 
          } 


          processinfo(); 
         } 
        }; 
        LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(locationChangereceiver, 
          new IntentFilter(LocationChangeIntent.ACTION_LOCATION_CHAHGE)); 
        startService(new Intent(this, LocationService.class)); 










      } else if (resultCode == RESULT_CANCELED) { 
       // Handle cancel 
       Log.e(TAG, "There's a problem with the scan. Scan result failed"); 
       Toast.makeText(this, "There's a problem with the scan. Scan result failed", 
         Toast.LENGTH_LONG).show(); 
      } 
     } 
    } 

回答

1

解決了。我只在服務停止後處理信息。爲此,我檢查了服務是否已經停止使用

if(stopService(new Intent(context, LocationService.class))){ 

processInfo() 
} 

as stopservice returns a boolean。