Reputation: 688
public class ValuatorService extends Service {
SQLLiteAdapter db;
Context context;
String strLat, strLong, strTempLat, strTempLong;
static String prospect_no;
public Database database;
long boolDistanceDetail;
static double dist, roundOff;
SQLLiteAdapter adapter;
public ArrayList<String> latDetails;
private ArrayList<Double> distanceList;
static int loop = 0;
public ValuatorService() {
}
@Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
db = new SQLLiteAdapter(this);
db.open();
context = getApplicationContext();
database = Database.getDBAdapterInstance(context);
distanceList = new ArrayList<>();
}
@SuppressLint("NewApi")
@Override
public void onStart(Intent intent, int startId) {
// Perform your long running operations here.
long boolDistanceDetail = db.insertDistanceDetail(Util
.getstringvaluefromkey(MenuScreen.activity, "LoginName"), Util
.getstringvaluefromkey(MenuScreen.activity, "trackid"), Util
.getstringvaluefromkey(MenuScreen.activity, "SimNo"), Util
.getstringvaluefromkey(MenuScreen.activity, "req_no"), Util
.getstringvaluefromkey(MenuScreen.activity, "customer_name"),
Util.getstringvaluefromkey(MenuScreen.activity, "cus_mobile"),
Util.getstringvaluefromkey(MenuScreen.activity, "city"), Util
.getstringvaluefromkey(MenuScreen.activity,
"prospect_no"));
Log.e("Tab1 Insert", String.valueOf(boolDistanceDetail));
prospect_no = Util.getstringvaluefromkey(MenuScreen.activity,
"prospect_no");
loop = 0;
getLatLong();
}
@Override
public void onDestroy() {
db.sumDistance();
db.close();
}
public void getLatLong() {
loop++;
LocationResult locationResult = new MyLocation.LocationResult() {
@Override
public void gotLocation(Location location) {
try {
strLat = String.valueOf(location.getLatitude());
strLong = String.valueOf(location.getLongitude());
if (loop == 1) {
boolDistanceDetail = db.insertLatLong(prospect_no,
strLat, strLong, "0");
strTempLat = strLat;
strTempLong = strLong;
} else if (loop > 1) {
boolDistanceDetail = db.insertLatLong(
prospect_no,
strLat,
strLong,
String.valueOf(distFrom(
Float.parseFloat(strTempLat),
Float.parseFloat(strTempLong),
Float.parseFloat(strLat),
Float.parseFloat(strLong))));
strTempLat = strLat;
strTempLong = strLong;
Log.e("distFrom",
String.valueOf(distFrom(
Float.parseFloat(strTempLat),
Float.parseFloat(strTempLong),
Float.parseFloat(strLat),
Float.parseFloat(strLong))));
}
Log.e("Tab1 Insert", String.valueOf(boolDistanceDetail));
Log.e("OldLat - OldLong", strTempLat + " - " + strTempLong);
Log.e("Lat - Long", strLat + " - " + strLong);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
getLatLong();
distanceList.add(roundOff);
Log.d("ValuatorService", "" + distanceList);
}
}, 10000);
} catch (Exception ex) {
Log.e("Error", ex.toString());
}
}
};
MyLocation myLocation = new MyLocation();
myLocation.getLocation(context, locationResult);
}
private static float distFrom(float fltFromLat, float fltFromLong,
float fltToLat, float fltToLong) {
double earthRadius = 6371000; // meters
double dLat = Math.toRadians(fltToLat - fltFromLat);
double dLng = Math.toRadians(fltToLong - fltFromLong);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
+ Math.cos(Math.toRadians(fltFromLat))
* Math.cos(Math.toRadians(fltToLat)) * Math.sin(dLng / 2)
* Math.sin(dLng / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
dist = (float) ((earthRadius * c) / 1000);
roundOff = Math.round(dist * 100.0) / 100.0;
return (float) roundOff;
}
}
After stopping the service I'm receiving an error in log cat :
java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/com.mahindra.tev/databases/TravelDistance.db
Upvotes: 0
Views: 43
Reputation: 3818
This means you're asynchronously reading/writing from database, and once the onDestroy() is called it's impossible to access it because you closed it.
A simple solution would be to not to call db.close()
inside onDestroy()
method, but only when you're actually done with it.
Another solution would be of checking if db.isOpen()
before actually inserting/updating rows.
Upvotes: 1