I have an app that scans a qrcode to get client data using the ZXing library. The qrcode holds longitude and latitude data. When the scan takes place i can get the info from the scan result. This is inside the onActivityResult of the ZXing scan. Inside onActivityResult i start a service that find sthe user location, i then compare the results from the service to those held on the qrcode. The results of the scan and the lon and lat values from the service are then stored on a sqlite DB on the phone, this is called a transaction. if the DB is empty then the transaction is stored to the DB. if there is already a transaction in the DB then the service runs twice, even when i have already called stopService. Can any on tell me why.
I’ve logged out that the service is destroyed, is there any reason a service can run after it has been destroyed? thanks.
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();
}
}
}
solved. I only processed the info after the service had stopped. To do this I checked to see if the service had stopped by using
as stopservice returns a boolean.