onLocationChanged 内容应该是什么

what should be the onLocationChanged content

我知道这个问题在SO里被问过很多次了,我试过关注了20个网页内容.. page 1 page 2 page 3 page 4 等等等等页面...

基本上我的代码source是Androidhive.info

我的问题是——在 log cat 中它显示了位置但它没有保存在我的数据库中..

我在服务器上有一个 php 文件,我在单击按钮时通过 httppost 调用它。我的座右铭是

1.On 按钮点击它应该发送位置(此任务完成)

2.App 应该 运行 在后台并按时间间隔发送位置详细信息(任务待定)

按照 SO 中的建议,我已经扩展了服务,但我没有在这里使用任何警报管理器或作业调度程序

这让我的代码变得笨拙..

所以我的问题是应该在onLocationchanged方法中编写什么代码,以便它会在位置更改时向服务器发送数据(程序将 运行 每 n 分钟)

代码片段::

1.GPSTracker.java

public class GPSTracker extends  Service implements LocationListener
{
statements;
} 

2.some 参与 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;
          }
        }

任何建议都会有很大帮助..

如果需要更多代码片段,请告知

我在 Android 工作室做这个项目

日志::

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

它每分钟都在日志中给我位置更新..但是这个数据没有发送到数据库

我在 doInBackground() 中编写了 http 调用,因此控件不会传递给该方法...它再也不会被调用..

编辑 1

当我使用待定意图时,此屏幕是输出,即使我的 GPS 设置已启用

添加代码::

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;
    }

都没有locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,60000,0, this); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,60000,0, pi); 也没有做到这一点

onLocationChanged 内容。

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

LongOperation 是具有 http call

AsyncTask class
               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());
                    }