问题描述
我正在尝试使用以下代码获取我的 G1 的 GPS 位置
I am trying to get the GPS location of my G1 using the following code
活动中
MyLocationListener myListener = new MyLocationListener();
LocationManager myManager = (LocationManager)getSystemService(LOCATION_SERVICE);
myManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 0, myListener);
这是 LocationListener 类
This is the LocationListener class
public class MyLocationListener implements LocationListener {
private static double latitude;
private static double longitude;
@Override
public void onLocationChanged(Location arg0) {
latitude = arg0.getLatitude();
longitude = arg0.getLongitude();
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
public static double getLatitude() {
return latitude;
}
public static double getLongitude() {
return longitude;
}
}
我等待了长达 30 秒,但没有调用任何侦听器方法.GPS图标出现了,在那里停留了一段时间,但由于某种原因我没有得到修复,即使在户外明亮的阳光下也是如此.我正在使用 1.5 SDK 在 G1 上进行测试.
I waited for as long as 30 seconds but none of the listener methods were called. The GPS icon shows up, stays there for some time but for some reason I don't get a fix, even outdoors in bright sunlight. I am testing on a G1 with 1.5 SDK.
有人可以告诉我代码有什么问题吗?谢谢.
Can someone please tell me what's wrong with the code? Thanks.
添加日志
06-02 18:30:43.143: ERROR/System(52): java.lang.SecurityException
06-02 18:30:43.143: ERROR/System(52): at android.os.BinderProxy.transact(Native Method)
06-02 18:30:43.143: ERROR/System(52): at android.os.ServiceManagerProxy.addService(ServiceManagerNative.java:146)
06-02 18:30:43.143: ERROR/System(52): at android.os.ServiceManager.addService(ServiceManager.java:72)
06-02 18:30:43.143: ERROR/System(52): at com.android.server.ServerThread.run(SystemServer.java:155)
06-02 18:30:43.152: ERROR/AndroidRuntime(52): Crash logging skipped, no checkin service
06-02 18:30:43.382: ERROR/SystemServer(52): Failure starting StatusBarService
06-02 18:30:43.382: ERROR/SystemServer(52): java.lang.NullPointerException
06-02 18:30:43.382: ERROR/SystemServer(52): at com.android.server.status.StatusBarPolicy.updateBluetooth(StatusBarPolicy.java:749)
06-02 18:30:43.382: ERROR/SystemServer(52): at com.android.server.status.StatusBarPolicy.<init>(StatusBarPolicy.java:282)
06-02 18:30:43.382: ERROR/SystemServer(52): at com.android.server.status.StatusBarPolicy.installIcons(StatusBarPolicy.java:337)
06-02 18:30:43.382: ERROR/SystemServer(52): at com.android.server.ServerThread.run(SystemServer.java:186)
06-02 18:30:43.382: ERROR/AndroidRuntime(52): Crash logging skipped, no checkin service
推荐答案
首先,在你调用 requestLocationUpdates()
的活动中,第二个参数是最短时间,而不是预定时间时间.您目前要求最多每 2 秒更新一次.它实际上不会报告任何内容,直到它可以...有时需要一点时间.
Firstly, in your activity, where you're calling requestLocationUpdates()
, the second argument is the minimum time, not the scheduled time. You're currently asking for an update AT MOST every 2 seconds. It won't actually report anything until it can... sometimes it takes a little while.
如果您的 GPS 在其他地方(例如在地图中)也没有响应/速度很慢,请尝试重新启动手机(有些人声称您必须关闭手机然后拔出电池才能完全关闭 GPS 无线电).希望这将使它恢复到正常的响应状态.
If your GPS is unresponsive/slow elsewhere as well (like in Maps) try restarting the phone (some people claim you have to turn off the phone then pull out the battery to completely de-energized the GPS radio). Hopefully this will bring it back up to it's normally responsive state.
另外,这不重要,但您说您使用的是 1.5 SDK,但您没有提及您是否针对它进行编译(而不是 1.1).我之所以提出这个只是因为 GPS 在 Cupcake 中运行得更好/更快,所以我想知道你的手机实际上在运行什么.
Also, it shouldn't matter, but you said you were using the 1.5 SDK, but you didn't mention if you were compiling against it (as opposed to 1.1). I only bring this up because the GPS works better/faster in Cupcake, so I was wondering what your phone was actually running.
更新:
我自己写了一个小测试,看看我是否可以复制你的问题.它仅包含三个用户生成的文件,我使用 SDK 1.1 和 1.5 对其进行了测试
I wrote up a little test on my own to see if I could duplicate your problem. It consists of just three user-generated files and I tested it with SDK 1.1 and 1.5
在主Activity中(我只是简单的使用了Main.java
):
In the main Activity (I simply used Main.java
):
package org.example.LocationTest;
import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class Main extends Activity implements LocationListener{
private LocationManager myManager;
private TextView tv;
/**********************************************************************
* Activity overrides below
**********************************************************************/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// get a handle to our TextView so we can write to it later
tv = (TextView) findViewById(R.id.TextView01);
// set up the LocationManager
myManager = (LocationManager) getSystemService(LOCATION_SERVICE);
}
@Override
protected void onDestroy() {
stopListening();
super.onDestroy();
}
@Override
protected void onPause() {
stopListening();
super.onPause();
}
@Override
protected void onResume() {
startListening();
super.onResume();
}
/**********************************************************************
* helpers for starting/stopping monitoring of GPS changes below
**********************************************************************/
private void startListening() {
myManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
0,
0,
this
);
}
private void stopListening() {
if (myManager != null)
myManager.removeUpdates(this);
}
/**********************************************************************
* LocationListener overrides below
**********************************************************************/
@Override
public void onLocationChanged(Location location) {
// we got new location info. lets display it in the textview
String s = "";
s += "Time: " + location.getTime() + "
";
s += " Latitude: " + location.getLatitude() + "
";
s += " Longitude: " + location.getLongitude() + "
";
s += " Accuracy: " + location.getAccuracy() + "
";
tv.setText(s);
}
@Override
public void onProviderDisabled(String provider) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
}
在您的主"布局文件中(我使用了 main.xml
):
In your "main" layout file (I used main.xml
):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/TextView01"
android:text="Waiting for location information..."
/>
</LinearLayout>
然后,在您的 AndroidManifest.xml
中:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.example.LocationTest"
android:versionCode="1"
android:versionName="1.0">
<application
android:icon="@drawable/icon"
android:label="@string/app_name">
<activity
android:name=".Main"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="3" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
</manifest>
你应该能够编译/运行它就好了.如果,使用这个,你仍然无法得到修复,这不是软件问题.要么你的 GPS 不好,要么就是无法锁定任何卫星.
You should be able to compile/run this just fine. If, using this, you still can't get a fix it's not a software problem. You either have a bad GPS or it just can't get a lock on any satellites.
您可以在这里下载源代码.
这篇关于Android - 使用 LocationManager 不会提供地理修复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!