Android 如何在查询字符串中发送我的GPS位置(纬度、经度)?

Android 如何在查询字符串中发送我的GPS位置(纬度、经度)?,android,gps,query-string,Android,Gps,Query String,我需要为android制作一个简单的应用程序,每25秒将我智能手机的位置发送到一个Web应用程序。我的Web应用处于联机状态,现在我可以手动传递值,如下所示: 我是Android开发的绝对初学者,所以请尝试一步一步地向我解释。首先获取位置: LocationManager locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE); locMan.requestLocationUpdates( Locati

我需要为android制作一个简单的应用程序,每25秒将我智能手机的位置发送到一个Web应用程序。我的Web应用处于联机状态,现在我可以手动传递值,如下所示:

我是Android开发的绝对初学者,所以请尝试一步一步地向我解释。

首先获取位置:

LocationManager locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locMan.requestLocationUpdates(
    LocationManager.GPS_PROVIDER, 0, 0, location listener);
第一个零表示位置更新的时间间隔 第二个表示更新的距离间隔。。按照您的意愿进行配置

在听众中:

public void onLocationChanged(Location location) {
if (location != null) {
    double lat = location.getLatitude();
    double long = location.getLongitude();
   }
}
您只需使用该链接发出http请求,如下所示:

String url = "http://mywebapp.com/coordinates/createlatitude=" + lat +"&longitude=" + long;
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
httpClient.execute(httpGet);

在做了深入的研究之后,我发现我认为最简单的方法是在查询字符串中发送GPS位置(纬度、经度)。也许网络上没有比这更短的代码了。因此,我们将分两步进行:

  • 首先将此权限添加到
    AndroidManifest.xml

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    

  • 注意:如您所见,我使用的是
    new Thread(){public void run(){/*code here*/}.start()
    在函数
    SendQueryString()
    中,这是因为我们试图向服务器发出请求,所以在Android中,您不能在主线程中向服务器发出请求,因为图形和主进程都在主线程中。事实上,如果您试图在主线程中发出请求,它将返回一个错误。

    Avast告诉您的网页是有害的:(以这种粗暴的方式获取位置不是一种好的做法。即使GPS提供商也可以以不可预测的顺序发送位置更新,我的意思是,它可以以10米的精度发送位置,一秒钟后以50米的精度发送位置。实时接收位置时需要进行某种附加检查。这是一个非常有效的点。这些片段只是为了显示概念和不完全是这样使用的。时间和距离的准确性可以根据他的配置和cource更改。更好的做法是订阅网络和GPS提供商,以跟踪用户无法使用或禁用GPS的情况。好的,马哈茂德,我会尝试,如果有效,我会给你分数。马哈茂德·埃尔莫rabea我试图测试你的代码,但是代码中有很多错误,请你自己试试,然后再复制。
    public class MainActivity extends Activity {
    
        public double latitude;
        public double longitude;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            /* Use the LocationManager class to obtain GPS locations */
            LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
            LocationListener mlocListener = new MyLocationListener();
            mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
        }
    
        /* Class My Location Listener */
    
        public class MyLocationListener implements LocationListener {
    
            @Override
            public void onLocationChanged(Location loc) {
                latitude = loc.getLatitude();
                longitude = loc.getLongitude();
                String Text = "My current Latitude = " + latitude  + " Longitude = " + longitude;
                Toast.makeText( getApplicationContext(),Text,Toast.LENGTH_SHORT).show();
    
                SendQueryString(); // for send the  Query String of latitude and logintude to the webapp.
            }
    
    
    
            @Override
            public void onProviderDisabled(String provider) {   
                Toast.makeText( getApplicationContext(),"Gps Disabled",Toast.LENGTH_SHORT ).show();
            }
    
    
            @Override
            public void onProviderEnabled(String provider) {
                Toast.makeText( getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();
            }
    
    
            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
            }
    
        }/* End of Class MyLocationListener */
    
    
        public void SendQueryString() {
            new Thread() {  
                public void run() {
    
                    String url = "http://mywebapp.com/coordinates/create?latitude=" + latitude +"&longitude=" + longitude;
    
                    try {
                        HttpClient Client = new DefaultHttpClient();
                        HttpGet httpget = new HttpGet(url);
                        Client.execute(httpget);
                    }
                    catch(Exception ex) {
                        String fail = "Fail!";
                        Toast.makeText( getApplicationContext(),fail,Toast.LENGTH_SHORT).show();
                    }
                }
            }.start();
        }
    }