Android Programming:
I am trying to make a background service which runs even when app is closed, it would get current location and once person is close to destination the app would start up.
[CODE]package app.wakeup;
import com.google.android.gms.maps.model.LatLng;
import android.app.Service;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class Awake_Alarm extends Service implements LocationListener{
Location location;
ConversePrefs cp;
ConversePrefs_sets cpss;
LatLng final_dest;
Settings sets;
double final_range;
LocationManager locationManager;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
final String provider = locationManager.getBestProvider(criteria, true);
location = locationManager.getLastKnownLocation(provider);
locationManager.requestLocationUpdates(provider, 20000, 0, this);
cp = new ConversePrefs(this);
cpss = new ConversePrefs_sets(this);
sets = new Settings(this);
final_range = cp.GetIntSetting("Range", 250);
final_dest = cp.GetCoords();
if(cpss.GetIntSetting(sets.def_units_name, 0) == 1){
final_range = final_range * 1.09361;
}
Toast.makeText(getApplicationContext(), "Started", Toast.LENGTH_LONG).show();
Log.d("STATUS", "RUNNING");
super.onCreate();
}
public double distanceGet(LatLng StartP, LatLng EndP) {
double lat1 = StartP.latitude;
double lat2 = EndP.latitude;
double lon1 = StartP.longitude;
double lon2 = EndP.longitude;
double dLat = Math.toRadians(lat2-lat1);
double dLon = Math.toRadians(lon2-lon1);
double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2);
double c = 2 * Math.asin(Math.sqrt(a));
return 6366000 * c;
}
@Override
public void onLocationChanged(Location arg0) {
LatLng me = new LatLng(location.getLatitude(), location.getLongitude());
double dest = distanceGet(me, final_dest);
if(dest <= final_range){
Toast.makeText(getApplicationContext(), String.valueOf(dest)+" Away-.", Toast.LENGTH_LONG).show();
Log.d("STATUS", "RUNNING-"+String.valueOf(dest));
}
Toast.makeText(getApplicationContext(), String.valueOf(dest)+" Away2-.", Toast.LENGTH_LONG).show();
Log.d("STATUS", "RUNNING2-"+String.valueOf(dest));
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
}[/CODE]
It seems to work fine, it launches etc and runs, and when location is changed it makes Toast text as required, but: The distance to target doesn't get updated, say I launch app, click start service and distance to destination is 1500 Meters, when location gets changed it still makes toast text saying: "1500 meters"
What am I doing wrong?
I don't know the answer, but it's a good idea to put [JAVA] before the thread title or so :)
[QUOTE=Arxae;45300117]I don't know the answer, but it's a good idea to put [JAVA] before the thread title or so :)[/QUOTE]
done
I ment in the topic title :P
[QUOTE=Arxae;45301001]I ment in the topic title :P[/QUOTE]
cant change it for some reason
Sorry, you need to Log In to post a reply to this thread.