0

我有一个按钮,单击它需要从 gps 获取用户当前位置。

我可以在没有 LocationListener 的情况下做到吗?因为我只需要在点击操作后才能获得位置,而且我不需要一直通过 onLocationChanged 吃电池。可能吗?

我试过了,但有错误

public void goToHomePoint(View v){
    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);  
    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    Toast.makeText(getApplicationContext(), String.valueOf(location.getLatitude()) + " " + String.valueOf(location.getLongitude()), Toast.LENGTH_SHORT).show();
}

在清单中

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

错误

04-29 12:01:39.696: E/AndroidRuntime(252): Uncaught handler: thread main exiting due to uncaught exception
04-29 12:01:39.716: E/AndroidRuntime(252): java.lang.IllegalStateException: Could not execute method of the activity
04-29 12:01:39.716: E/AndroidRuntime(252):  at android.view.View$1.onClick(View.java:2031)
04-29 12:01:39.716: E/AndroidRuntime(252):  at android.view.View.performClick(View.java:2364)
04-29 12:01:39.716: E/AndroidRuntime(252):  at android.view.View.onTouchEvent(View.java:4179)
04-29 12:01:39.716: E/AndroidRuntime(252):  at android.view.View.dispatchTouchEvent(View.java:3709)
04-29 12:01:39.716: E/AndroidRuntime(252):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-29 12:01:39.716: E/AndroidRuntime(252):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-29 12:01:39.716: E/AndroidRuntime(252):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-29 12:01:39.716: E/AndroidRuntime(252):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
04-29 12:01:39.716: E/AndroidRuntime(252):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
04-29 12:01:39.716: E/AndroidRuntime(252):  at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
04-29 12:01:39.716: E/AndroidRuntime(252):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
04-29 12:01:39.716: E/AndroidRuntime(252):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
04-29 12:01:39.716: E/AndroidRuntime(252):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-29 12:01:39.716: E/AndroidRuntime(252):  at android.os.Looper.loop(Looper.java:123)
04-29 12:01:39.716: E/AndroidRuntime(252):  at android.app.ActivityThread.main(ActivityThread.java:4363)
04-29 12:01:39.716: E/AndroidRuntime(252):  at java.lang.reflect.Method.invokeNative(Native Method)
04-29 12:01:39.716: E/AndroidRuntime(252):  at java.lang.reflect.Method.invoke(Method.java:521)
04-29 12:01:39.716: E/AndroidRuntime(252):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-29 12:01:39.716: E/AndroidRuntime(252):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-29 12:01:39.716: E/AndroidRuntime(252):  at dalvik.system.NativeStart.main(Native Method)
04-29 12:01:39.716: E/AndroidRuntime(252): Caused by: java.lang.reflect.InvocationTargetException
04-29 12:01:39.716: E/AndroidRuntime(252):  at com.artox.relax.inf.MapViewActivity.goToHomePoint(MapViewActivity.java:141)
04-29 12:01:39.716: E/AndroidRuntime(252):  at java.lang.reflect.Method.invokeNative(Native Method)
04-29 12:01:39.716: E/AndroidRuntime(252):  at java.lang.reflect.Method.invoke(Method.java:521)
04-29 12:01:39.716: E/AndroidRuntime(252):  at android.view.View$1.onClick(View.java:2026)
04-29 12:01:39.716: E/AndroidRuntime(252):  ... 19 more
04-29 12:01:39.716: E/AndroidRuntime(252): Caused by: java.lang.NullPointerException
4

2 回答 2

0

我正在尝试的这段代码似乎对我有用,它与您的代码相同,但 getSystemService 的参数是Context.LOCATION_SERVICE

LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE );

Location location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

double lat = location.getLatitude();

double lon = location.getLongitude();

也就是说,它似乎获得了一个一次性的位置,尽管该位置可能是旧的/过时的

于 2012-05-17T01:26:08.167 回答
0

您可以编写一个 LocationListener,但是当您第一次收到一个位置时,如果准确度是您想要的和东西,那么您可以从 LocationManager 中删除相同的侦听器removeUpdates。您甚至可以在等待位置时使用进度对话框,并在您有足够好的位置以删除位置侦听器时将其关闭。

于 2012-04-28T14:40:12.267 回答