我认为这将是超级简单的 - 但这是花了更长的时间比它应该。当Android意图开始时获取GPS
我只是想获得GPS坐标去logcat,这就是字面意思。
意图着火。每次都会出现“Intent Fired”消息。该应用程序拒绝去位置更改事件。
如果有人可以看看,让我知道我失败的地方,这将是超级赞赏。
public class MyReceiver extends IntentService implements LocationListener {
private LocationManager locationManager;
public MyReceiver() {
super("MyReceiver");
}
protected void onHandleIntent(Intent intent) {
Log.i("DebugMe", "Intent Fired");
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
}
public void onLocationChanged(Location location) {
String lat = String.valueOf(location.getLatitude());
String lon = String.valueOf(location.getLongitude());
Log.i("DebugMe", lat + " " + lon);
locationManager.removeUpdates(this);
}
public void onProviderDisabled(String provider) {
Log.d("DebugMe ", "Disabled");
}
public void onProviderEnabled(String provider) {
Log.d("DebugMe","Enabled");
}
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("DebugMe","Status");
}
}
谢谢
“当您获得Location对象时,使用标准Service和执行stopSelf。” - 恕我直言,这是更好的方法。 – CommonsWare
它当然是 – maciekjanusz
嗯,感谢上帝,我问 - 我永远不会想到我自己。为什么我使用意向服务的原因是因为这个应用程序需要能够从adb采取命令,这个解决方案允许我从笔记本电脑上做“adb shell am startservice ...”...有没有另外一种方法来实现GPS从adb cmd行查找? – Nefariis