0

qrcodeをスキャンして、ZXingライブラリを使用してクライアントデータを取得するアプリがあります。qrcodeは経度と緯度のデータを保持します。スキャンが行われると、スキャン結果から情報を取得できます。これは、ZXingスキャンのonActivityResult内にあります。onActivityResult内で、ユーザーの場所を見つけるサービスを開始し、サービスの結果をqrcodeに保持されている結果と比較します。スキャンの結果とサービスからのlonおよびlat値は、電話のsqliteDBに保存されます。これはトランザクションと呼ばれます。DBが空の場合、トランザクションはDBに保存されます。DBにすでにトランザクションがある場合は、stopServiceを既に呼び出している場合でも、サービスは2回実行されます。理由を教えてもらえますか?

サービスが破棄されたことをログアウトしましたが、破棄された後にサービスを実行できる理由はありますか?ありがとう。

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 に答える