How to send updated long/lat to server in android? - android

In my application i want to get the updated location of the mobile user and i want to send it to the server continuously after periodic interval of certain time or after the user travels certain(say 500 meter) distance.I need these things to be done in backgroung.I know for this i have to implement service class. But I am not getting exactly how to do this.I did some work on that.
Can anybody please help me in this issue.
I did following things in the service class.
public class BackGroundService extends Service implements LocationListener{
public static final String Tag = BackGroundService.class.getName();
LocationManager myLocationManager;
Location myLocation;
LocationListener myLocationListener;
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public void OnCreate()
{
super.onCreate();
Log.d(Tag, "Service Started");
android.os.Debug.waitForDebugger();
Criteria criteria = new Criteria();
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAccuracy(Criteria.ACCURACY_LOW);
String locationProvider = myLocationManager.getBestProvider(criteria, true);
myLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
myLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000*60*5, 500, myLocationListener);
myLocation = myLocationManager.getLastKnownLocation(locationProvider);
}
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
longitude = location.getLongitude();
latitude = location.getLatitude();
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Log.d(Tag, "Provider is disabled");
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Log.d(Tag, "Location Provider is enabled");
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
From here I want know how can i get current lat/long of user and send it to the server.

Answer for my question is.......
public class LocationService extends Service{
#Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
final LocationManager mlocmag = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
final LocationListener mlocList = new MyLocationList();
final Location loc = mlocmag.getLastKnownLocation(LocationManager.GPS_PROVIDER);
UpdateWithNewLocation(loc); // This method is used to get updated location.
mlocmag.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocList);
}
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
#Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
}
private void UpdateWithNewLocation(final Location loc) {
// TODO Auto-generated method stub
if(loc!= null)
{
final double lat =loc.getLatitude(); // Updated lat
final double Long = loc.getLongitude(); // Updated long
ConnectMySQL obj = new ConnectMySQL();
obj.call(lat,Long); // Call this method when location is updated and save the data.
}
else
{
String latLongStr = "No lat and longitude found";
Toast.makeText(this, "Your location is "+latLongStr ,Toast.LENGTH_LONG).show();
}
}
public class MyLocationList implements LocationListener
{
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
UpdateWithNewLocation(arg0);
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),"GPS Disable ", Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),"GPS enabled", Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}

Related

find lat and lon in android app

i need to get the lat and lon in a activity. In my oncreate method I tried the code below but im not getting anything when i try to print the Lat. What am I doing wrong. I have included in the manifest file too. I need two strings with the value of Lat and lon
LocationManager manager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
LocationListener lis = new LocationListener(){
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
Log.d("", "location " + location.getLatitude());
}
#Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
};
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, lis);

LocationManager.requestLocationUpdates not responding

I am trying to make an app where an intent service is running in the background and getting the location of the user by the network provider, and when the user is close to his destination(a phone-call gate) its starts another IntentService for the specific gate and get location updates more often by the gps provider.
Problem is when I try to get the location on the more active intentservice it doesn't respond at all.
here is the code in my onStartCommand:
Bundle gateData = intent.getExtras();
gateLat = gateData.getDouble("latitude");
gateLong = gateData.getDouble("longitude");
gateName = gateData.getString("gateName");
gatePhoneNumber = gateData.getString("gatePhoneNumber");
gateId = gateData.getInt("gateId");
Log.i("Active onStartCommand for:", gateId+"\n"+gateName+"\n phone:"+gatePhoneNumber);
helper = new MyGatesOpenHelper(this);
currentGate = helper.getGate(gateId);
currentGate.setMode(Gate.MODE_ONHOLD);
helper.updateGate(currentGate);
results = new float[3];
aManager = (LocationManager)getSystemService(LOCATION_SERVICE);
aListener = new LocationListener() {
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
makeUseOfNewLocation(location);
Log.i("onLocationChanged coords","Lat:"+location.getLatitude()+" Long:"+location.getLongitude());
}
};
boolean isGpsEnabled = aManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
Log.i("GPS Status:",isGpsEnabled?"Enabled":"Disabled");
aManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, aListener);
return super.onStartCommand(intent, flags, startId);

Fix decimal numbers

Now I;m receiving the GPS location data from android application, and send it to Labview.
However, the decimal number of gps location value changes due to the location.
I want to fix the decimal number such as 7
For example, what I get is 32.33333 but I need 32.333330
How can I declare the value?
Here is the gps value part
private class MyLocationListener implements LocationListener
{
private String gps2;
#Override
public void onLocationChanged(Location loc) {
if (loc != null) {
/* 화면의 상단에 위치한 TextView에 위도, 경도를 출력함*/
gps.setText("La:"+loc.getLatitude()+", Lo:"+loc.getLongitude());
gps2 = gps.getText().toString();
Main.getInstance().sendMessage(gps2);
}
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
}
}
try doing this way
NumberFormat numberFormat = new DecimalFormat("#.000000");
gps.setText("La:"+numberFormat.format(loc.getLatitude())+", Lo:"+numberFormat.format(loc.getLongitude()));

Updating Activity with Latitude/Longitude values returned from a Service

I'm trying to write a simple app that updates the MainActivity with the Lat/Lng values returned by the service. But it always returns the null value. I have added permissions and added TheService.java as service in AndroidManifest.xml file...Kindly tell me where I have gone wrong.
MainActivity.java
public class MainActivity extends Activity {
TextView tv1, tv2;
IntentFilter filter;
receive rec;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 =(TextView)findViewById(R.id.textView1);
tv2 = (TextView)findViewById(R.id.textView2);
filter = new IntentFilter("Updated");
rec = new receive();
Intent gps_int = new Intent(this,TheService.class);
startService(gps_int);
}
#Override
public void onPause()
{
super.onPause();
unregisterReceiver(rec);
}
#Override
public void onResume()
{
super.onResume();
rec = new receive();
registerReceiver(rec, filter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class receive extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
tv1.setText(intent.getExtras().getString("lat"));
tv2.setText(intent.getExtras().getString("lon"));
Toast.makeText(getApplicationContext(), "Toast Executed", Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "BR Latitude "+intent.getExtras().getString("lat"),Toast.LENGTH_SHORT).show();
}
}}
TheService.java
public class TheService extends Service implements LocationListener {
LocationManager lm;
Location loc;
double lat = 0;
double lon = 0;
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
lm=(LocationManager)getSystemService(LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, this);
loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
onLocationChanged(loc);
return START_STICKY;
}
#Override
public void onCreate()
{
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
updateui(location);
}
private void updateui(Location location) {
// TODO Auto-generated method stub
lat = location.getLatitude();
lon = location.getLongitude();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent gps_intent = new Intent("Updated");
gps_intent.putExtra("lat", lat);
gps_intent.putExtra("lon", lon);
sendBroadcast(gps_intent);
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}}
You should use the Google Play Services for the location, that's easier to handle.
What for do you even need a Service? Having a location listener in your Activity is totally fine.
If you want to stay with the Service, than bind the Activity to it, instead of using a Broadcast.

native_start failed in startNavigating android GPS

I am new to android development. I am trying to get the Location using GPS in android. I have adopted two classes: one for the GPSTracker and the other one for the Activity. The location is getting set as null everytime and when i checked in LogCat, i get the following error:
native_start failed in startNavigating
I have set the permission in the manifest for GPS data as well. I even tried to pass fake latitude and longitude by using the emulator control, to no effect. what might be the reason? I am coding for the Android 2.3
My code is: //GPSTracker.java
public class GPSTracker extends Service implements LocationListener {
private final Context mcontext;
boolean isGPSEnabled = false;
boolean canGetLocation = false;
double latitude,longitude;
Location location;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;
protected LocationManager locationManager;
protected LocationListener listener;
public GPSTracker(Context context){
this.mcontext = context;
getLocation();
}
public Location getLocation() {
try{
locationManager = (LocationManager) mcontext.getSystemService(LOCATION_SERVICE);
listener = new LocationListener() {
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onLocationChanged(Location location1) {
// TODO Auto-generated method stub
location = location1;
}
};
isGPSEnabled = locationManager.isProviderEnabled(locationManager.GPS_PROVIDER);
if(isGPSEnabled){
this.canGetLocation = true;
locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, listener);
Log.d("GPS", "Enabled");
if(locationManager!=null){
location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
if(location!=null){
latitude = location.getLatitude();
longitude = location.getLongitude();
//Log.d("Latitude", latitude.toString());
//Log.d("Longitude", longitude.toString());
}
else{
Log.d("Location","Null");
}
}
}
}catch(Exception e){
e.printStackTrace();
}
return location;
// TODO Auto-generated method stub
}
public boolean canGetLocation(){
return this.canGetLocation;
}
public void showSettingsAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mcontext);
alertDialog.setTitle("GPS Settings");
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mcontext.startActivity(intent);
}
});
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
#Override
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
}
#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
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
public double getLatitude() {
// TODO Auto-generated method stub
if(location!=null){
Log.d("Location",location.toString());
latitude = location.getLatitude();
}
return latitude;
}
public double getLongitude() {
// TODO Auto-generated method stub
if(location!=null){
Log.d("Location",location.toString());
longitude = location.getLongitude();
}
return longitude;
}
}
//LocationActivity.java
public class LocationActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
GPSTracker gps = new GPSTracker(LocationActivity.this);
if(gps.canGetLocation() == false){
Log.d("GPSLogFromActivity","False");
gps.showSettingsAlert();
}
else{
double latitude = gps.getLatitude();
System.out.println("Latitude: "+latitude);
double longitude = gps.getLongitude();
System.out.println("Longitud: e"+longitude);
Log.d("GPSLogFromActivity","True");
Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
}
}
});
hey friend have look on this
My current location always returns null. How can I fix this?

Categories

Resources