我有一个按钮,点击它需要从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
答案 0 :(得分:0)
您可以编写LocationListener,但是当您第一次收到位置时,如果准确度是您想要的东西,那么您可以使用removeUpdates
从LocationManager中删除相同的侦听器。您甚至可以在等待位置时使用进度对话框,并在有足够位置来移除位置监听器时将其关闭。
答案 1 :(得分: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();
那说它似乎有一个一次性的位置,虽然位置可能是旧的/过时的