我有一个应用程序扫描qrcode以使用ZXing库获取客户端数据。 qrcode保存经度和纬度数据。扫描发生时,我可以从扫描结果中获取信息。这是在ZXing扫描的onActivityResult内部。在onActivityResult内部,我启动了一个找到用户位置的服务,然后我将服务的结果与qrcode上的结果进行比较。然后将扫描结果以及来自服务的lon和lat值存储在手机上的sqlite DB上,这称为事务。如果DB为空,则将事务存储到DB。如果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();
}
}
}
答案 0 :(得分:1)
解决。我只在服务停止后处理了信息。为此,我检查了服务是否已停止使用
if(stopService(new Intent(context, LocationService.class))){
processInfo()
}
as stopservice返回一个布尔值。