Fixed some exceptions. Don't release app if backround service runs.

This commit is contained in:
Pavol Zibrita 2011-09-10 08:06:34 +02:00
parent 41787d7e1e
commit 3852e55aa5
3 changed files with 39 additions and 18 deletions

View file

@ -49,6 +49,7 @@ public class NavigationService extends Service implements LocationListener {
private static WakeLock lockStatic;
private PendingIntent pendingIntent;
private BroadcastReceiver broadcastReceiver;
@Override
public IBinder onBind(Intent intent) {
@ -106,13 +107,14 @@ public class NavigationService extends Service implements LocationListener {
}
// registering icon at top level
registerReceiver(new BroadcastReceiver() {
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
NavigationService.this.stopSelf();
}
}, new IntentFilter(OSMAND_STOP_SERVICE_ACTION));
};
registerReceiver(broadcastReceiver, new IntentFilter(OSMAND_STOP_SERVICE_ACTION));
Intent notificationIntent = new Intent(OSMAND_STOP_SERVICE_ACTION);
Notification notification = new Notification(R.drawable.icon, "", //$NON-NLS-1$
System.currentTimeMillis());
@ -150,6 +152,10 @@ public class NavigationService extends Service implements LocationListener {
// remove notification
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotificationManager.cancel(NOTIFICATION_SERVICE_ID);
if (broadcastReceiver != null) {
unregisterReceiver(broadcastReceiver);
broadcastReceiver = null;
}
}
@ -161,7 +167,10 @@ public class NavigationService extends Service implements LocationListener {
// unregister listener and wait next time
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.removeUpdates(this);
getLock(this).release();
WakeLock lock = getLock(this);
if (lock.isHeld()) {
lock.release();
}
}
savingTrackHelper.insertData(location.getLatitude(), location.getLongitude(), location.getAltitude(),
location.getSpeed(), location.getAccuracy(), location.getTime(), settings);

View file

@ -212,12 +212,15 @@ public class MainMenuActivity extends Activity {
closeButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
((OsmandApplication) activity.getApplication()).closeApplication();
getMyApplication().closeApplication();
//moveTaskToBack(true);
activity.finish();
//TODO this is different from MapActivity close...
if (getMyApplication().getNavigationService() == null) {
//http://stackoverflow.com/questions/2092951/how-to-close-android-application
System.runFinalizersOnExit(true); //if any threads are running, they will prevent app from exit
System.exit(0); //exit
System.runFinalizersOnExit(true);
System.exit(0);
}
}
});
@ -235,7 +238,7 @@ public class MainMenuActivity extends Activity {
}
startProgressDialog = new ProgressDialog(this);
((OsmandApplication)getApplication()).checkApplicationIsBeingInitialized(this, startProgressDialog);
getMyApplication().checkApplicationIsBeingInitialized(this, startProgressDialog);
SharedPreferences pref = getPreferences(MODE_WORLD_WRITEABLE);
boolean firstTime = false;
if(!pref.contains(FIRST_TIME_APP_RUN)){
@ -306,7 +309,7 @@ public class MainMenuActivity extends Activity {
}
protected void checkVectorIndexesDownloaded() {
MapRenderRepositories maps = ((OsmandApplication) getApplication()).getResourceManager().getRenderer();
MapRenderRepositories maps = getMyApplication().getResourceManager().getRenderer();
SharedPreferences pref = getPreferences(MODE_WORLD_WRITEABLE);
boolean check = pref.getBoolean(VECTOR_INDEXES_CHECK, true);
// do not show each time
@ -339,6 +342,10 @@ public class MainMenuActivity extends Activity {
}
private OsmandApplication getMyApplication() {
return (OsmandApplication) getApplication();
}
@Override
protected Dialog onCreateDialog(int id) {
if(id == OsmandApplication.PROGRESS_DIALOG){

View file

@ -239,20 +239,25 @@ public class SavingTrackHelper extends SQLiteOpenHelper {
public void insertData(double lat, double lon, double alt, double speed, double hdop, long time, OsmandSettings settings){
if (time - lastTimeUpdated > settings.SAVE_TRACK_INTERVAL.get()*1000) {
SQLiteDatabase db = getWritableDatabase();
if (db != null) {
db.execSQL(updateScript, new Object[] { lat, lon, alt, speed, hdop, time });
}
execWithClose(updateScript, new Object[] { lat, lon, alt, speed, hdop, time });
lastTimeUpdated = time;
}
}
public void insertPointData(double lat, double lon, long time, String description) {
execWithClose(updatePointsScript, new Object[] { lat, lon, time, description });
}
private void execWithClose(String script, Object[] objects) {
SQLiteDatabase db = getWritableDatabase();
try {
if (db != null) {
db.execSQL(updatePointsScript, new Object[] { lat, lon, time, description });
db.execSQL(script, objects);
}
} finally {
if (db != null) {
db.close();
}
}
}
}