简体   繁体   English

requestLocationUpdates根本不起作用

[英]requestLocationUpdates do not work at all

What i am trying to ping an url with device's gps location. 我正在尝试使用设备的gps位置ping URL。

in onCreate i have this 在onCreate我有这个

LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, locationListener);

and MyLocationListener class at below 和下面的MyLocationListener类

public class MyLocationListener implements LocationListener {
    private final String TAG = "Walkie.LocationListener";
    @Override
    public void onLocationChanged(Location loc) {
        String longitude = String.valueOf(loc.getLongitude());
        String latitude = String.valueOf(loc.getLatitude());
        try {
            URL url = new URL("http://url.com/gps.php?longitude=" + longitude + "&latitude=" + latitude);

            HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
            urlc.setRequestProperty("User-Agent", "Android Application");
            urlc.setRequestProperty("Connection", "close");
            urlc.setConnectTimeout(1000 * 30); // mTimeout is in seconds
            urlc.connect();
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Log.i(TAG, "Status changed");
    }

    @Override
    public void onProviderDisabled(String provider) {
        Log.i(TAG, "Provider disabled");
    }

    @Override
    public void onProviderEnabled(String provider) {
        Log.i(TAG, "Provider enabled");
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        Log.i(TAG, "Status changed");
    }

}

gps.php is not pinged at all, is there something i am doing wrong? gps.php根本不进行ping操作,我做错了什么吗?

I have those permissions in manifest 我在清单中有这些权限

android.permission.INTERNET android.permission.INTERNET

android.permission.ACCESS_NETWORK_STATE android.permission.ACCESS_NETWORK_STATE

android.permission.ACCESS_FINE_LOCATION android.permission.ACCESS_FINE_LOCATION

GPS has a cold start some time it may take several minutes if it is starting after a long time. GPS的启动时间很冷,如果启动时间过长,则可能需要几分钟。

Ensure you are having the GPS permission also in the manifest 确保清单中也有GPS许可

<uses-permission android:name="android.permission.ACCESS_GPS"/>

Also ensure it is enabled and in working state. 还要确保它已启用并处于工作状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM