what should be the onLocationChanged content

空扰寡人 提交于 2019-12-25 04:22:45

问题


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


回答1:


onLocationChanged content.

@Override
    public void onLocationChanged(Location location)
    {
        latitude = location.getLatitude();
        longitude = location.getLongitude();
        new LongOperation(latitude, longitude).execute();         
    }

LongOperation is an AsyncTask class which has http call

               try {
                        HttpClient httpclient = new DefaultHttpClient();
                        HttpPost httppost = new HttpPost("www.abc.com/kkk/ppp/nnn/update.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());
                    }


来源:https://stackoverflow.com/questions/28262338/what-should-be-the-onlocationchanged-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!