我想用设备的gps位置ping一个网址。
在onCreate中我有这个
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, locationListener);
和下面的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根本没有说明,我有什么问题吗?
我在清单中有这些权限
android.permission.INTERNET对
android.permission.ACCESS_NETWORK_STATE
android.permission.ACCESS_FINE_LOCATION
答案 0 :(得分:0)
如果长时间启动,GPS会在一段时间内冷启动可能需要几分钟。
确保您在清单
中也拥有GPS权限<uses-permission android:name="android.permission.ACCESS_GPS"/>
同时确保它已启用且处于工作状态。