0

我有一个使用 ZXing 库扫描 qrcode 以获取客户端数据的应用程序。二维码保存经度和纬度数据。当扫描发生时,我可以从扫描结果中获取信息。这是 ZXing 扫描的 onActivityResult 内部。在 onActivityResult 中,我启动了一个服务来查找用户位置,然后将来自服务的结果与保存在 qrcode 上的结果进行比较。扫描结果以及来自服务的 lon 和 lat 值然后存储在手机上的 sqlite DB 中,这称为事务。如果数据库为空,则事务将存储到数据库中。如果数据库中已经有事务,那么服务会运行两次,即使我已经调用了 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();
            }
        }
    }
4

1 回答 1

1

解决了。我只在服务停止后才处理信息。为此,我检查了服务是否已停止使用

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

processInfo()
}

因为 stopservice 返回一个布尔值。

于 2012-10-04T11:57:40.707 回答