I know that this question is asked many times in SO,and I've tried to follow 20 web page content.. page 1 page 2 page 3 page 4 etc etc pages...
Basically my code source is Androidhive.info
My issue is-- in log cat it is displaying the location but it is not saving in my database..
I have a php file at server and I am calling it by httppost on button click..My motto is
1.On button click it should send the location(this task completed)
2.App should run in background and send location details in time intervals(task pending)
As suggested in SO I've extended service but I am not using any alarm manager or job scheduler here
It is making my code clumsy..
So my question is what code should be written in onLocationchanged method so that it will send data to server whenever location is changed(program will run for every n minutes)
Code snippets::
1.GPSTracker.java
public class GPSTracker extends Service implements LocationListener
{
statements;
}
2.some part in GPSTracker.java
public void onLocationChanged(Location location)
{
latitude = location.getLatitude();
longitude = location.getLongitude();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,0, this);
Log.e("onLocationChanged",Double.toString(latitude));
Log.e("onLocationChanged",Double.toString(longitude));
}
3.MainActivity.java
btnShowLocation.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View arg0)
{
new LongOperation().execute();
}
}
4.MainActivity.java
private class LongOperation extends AsyncTask<String, Void, Void>
{
protected void onPreExecute()
{
}
protected Void doInBackground(String... urls)
{
ArrayList<NameValuePair> paramss = new ArrayList<NameValuePair>();
paramss.add(new BasicNameValuePair("latti", latti));
paramss.add(new BasicNameValuePair("longi", longi));
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http:///insertData.php");
httppost.setEntity(new UrlEncodedFormEntity(paramss));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("pass 1", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 1", e.toString());
Toast.makeText(getApplicationContext(), "Invalid IP Address",
Toast.LENGTH_LONG).show();
}
try
{
BufferedReader reader = new BufferedReader
(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result = sb.toString();
Log.e("pass 2", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 2", e.toString());
}
try
{
JSONObject json_data = new JSONObject(result);
code=(json_data.getInt("code"));
if(code==1)
{
FLAG=1;
}
else
{
FLAG=0;
}
}
catch(Exception e)
{
Log.e("Fail 3", e.toString());
}
return null;
}
}
Any suggestion would be of great help..
Kindly inform if any more code snippets are required
I am doing this project in Android studio
LOG::
02-02 10:52:22.180 15792-15792/com.example.mylocation E/onLocationChanged﹕ 17.3732771
02-02 10:52:22.180 15792-15792/com.example.mylocation E/onLocationChanged﹕ 78.5046526
02-02 10:52:24.050 15792-16248/com.example.mylocation E/pass 1﹕ connection success
02-02 10:52:24.070 15792-16248/com.example.mylocation E/pass 2﹕ connection success
02-02 10:53:22.130 15792-15792/com.example.mylocation E/onLocationChanged﹕ 17.3733159
02-02 10:53:22.130 15792-15792/com.example.mylocation E/onLocationChanged﹕ 78.5045958
02-02 10:54:22.070 15792-15792/com.example.mylocation E/onLocationChanged﹕ 17.3733133
02-02 10:54:22.070 15792-15792/com.example.mylocation E/onLocationChanged﹕ 78.5046106
02-02 10:55:22.120 15792-15792/com.example.mylocation E/onLocationChanged﹕ 17.373314
02-02 10:55:22.120 15792-15792/com.example.mylocation E/onLocationChanged﹕ 78.5046077
every minute it is giving me the location update in log..but this data is not sent to database
I've written http call in doInBackground() so the control is not passing to that method...it is never called again..
EDIT 1
when I used pending intent this screen is the output,even though my GPS settings are enabled
added code::
public Location getLocation()
{
try
{
intent = new Intent("com.example.dotweb.mylocation");
pi = PendingIntent.getBroadcast(getApplicationContext(),0, intent, 0);
locationManager = (LocationManager) mContext
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
} else {
this.canGetLocation = true;
if (isNetworkEnabled)
{
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,60000,0, this);
Log.d("Network", "Network");
if (locationManager != null)
{
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
if (isGPSEnabled) {
if (location == null)
{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,60000,0, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
neither locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,60000,0, this);
nor locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,60000,0, pi);
is doing the trick