Android的 - 使用LocationManager不给地缘修复 [英] Android - using LocationManager does not give a geo fix
问题描述
我想用下面的code,让我的G1的GPS位置
在活动
MyLocationListener myListener的=新MyLocationListener();
LocationManager myManager =(LocationManager)getSystemService(LOCATION_SERVICE);
myManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,2000,0,myListener的);
这是LocationListener的类
公共类MyLocationListener实现LocationListener的{
私有静态双重纬度;
私有静态双经度;
@覆盖
公共无效onLocationChanged(位置为arg0){
纬度= arg0.getLatitude();
经度= arg0.getLongitude();
}
@覆盖
公共无效onProviderDisabled(字符串提供商){
}
@覆盖
公共无效onProviderEnabled(字符串提供商){
}
@覆盖
公共无效onStatusChanged(字符串商,INT地位,捆绑演员){}
公共静态双getLatitude(){
返回纬度;
}
公共静态双getLongitude(){
返回经度;
}
}
我等了长达30秒,但没有一个监听方法被称为。该GPS图标显示,在那里停留了一段时间,但由于某种原因,我没有得到修复,即使在户外明亮的阳光下。我测试在G1 1.5 SDK。
有人能告诉我什么是错的code?谢谢你。
添加日志
06-02 18:30:43.143:错误/系统(52):java.lang.SecurityException异常
06-02 18:30:43.143:错误/系统(52):在android.os.BinderProxy.transact(本机方法)
06-02 18:30:43.143:错误/系统(52):在android.os.ServiceManagerProxy.addService(ServiceManagerNative.java:146)
06-02 18:30:43.143:错误/系统(52):在android.os.ServiceManager.addService(ServiceManager.java:72)
06-02 18:30:43.143:错误/系统(52):在com.android.server.ServerThread.run(SystemServer.java:155)
06-02 18:30:43.152:ERROR / AndroidRuntime(52):崩溃日志跳过,没有登记服务
06-02 18:30:43.382:ERROR / SystemServer(52):失败开始StatusBarService
06-02 18:30:43.382:ERROR / SystemServer(52):显示java.lang.NullPointerException
06-02 18:30:43.382:ERROR / SystemServer(52):在com.android.server.status.StatusBarPolicy.updateBluetooth(StatusBarPolicy.java:749)
。06-02 18:30:43.382:ERROR / SystemServer(52):在com.android.server.status.StatusBarPolicy< INIT>(StatusBarPolicy.java:282)
06-02 18:30:43.382:ERROR / SystemServer(52):在com.android.server.status.StatusBarPolicy.installIcons(StatusBarPolicy.java:337)
06-02 18:30:43.382:ERROR / SystemServer(52):在com.android.server.ServerThread.run(SystemServer.java:186)
06-02 18:30:43.382:ERROR / AndroidRuntime(52):崩溃日志跳过,没有登记服务
首先,在您的活动,在那里你调用 requestLocationUpdates()
,第二个参数是最小时间,未在预定的时间。您目前正在寻求更新至多每2秒。它实际上不会报告任何东西,直到它可以...有时需要一小会儿。
如果你的GPS没有反应/慢别处,以及(如在地图),请尝试重新启动手机(有些人声称,你必须关掉手机,然后拔出电池完全断电的GPS无线电)。希望这将使其恢复到它的正常响应状态。
此外,它不应该的问题,而是你说你正在使用的1.5 SDK,但你没有提到如果你编译反对(而不是1.1)。我只提这件事,因为全球定位系统更好地工作/在蛋糕快,所以我想知道你的手机,实际上运行的是什么。
更新:
我写了我自己的一个小测试,看看我是否可以复制你的问题。它由只有三个用户生成的文件和我一起SDK 1.1,并对其进行了测试1.5
在主要活动(我只是用 Main.java
):
包org.example.LocationTest;
进口android.app.Activity;
进口android.location.Location;
进口android.location.LocationListener;
进口android.location.LocationManager;
进口android.os.Bundle;
进口android.util.Log;
进口android.widget.TextView;
公共类主要活动扩展实现LocationListener的{
私人LocationManager myManager;
私人TextView的电视;
/ ******************* *********************
*活动将覆盖以下
************************************************** ****** /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
//得到一个处理我们的TextView的,所以我们可以在以后写它
电视=(TextView中)findViewById(R.id.TextView01);
//设置了LocationManager
myManager =(LocationManager)getSystemService(LOCATION_SERVICE);
}
@覆盖
保护无效的onDestroy(){
的stopListening();
super.onDestroy();
}
@覆盖
保护无效的onPause(){
的stopListening();
super.onPause();
}
@覆盖
保护无效onResume(){
startListening();
super.onResume();
}
/ ******************* *********************
*帮手启动/ GPS的监控停在下面的变化
************************************************** ****** /
私人无效startListening(){
myManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
0,
0,
本
);
}
私人无效的stopListening(){
如果(myManager!= NULL)
myManager.removeUpdates(本);
}
/ ******************* *********************
* LocationListener的覆盖之下
************************************************** ****** /
@覆盖
公共无效onLocationChanged(位置定位){
//我们得到了新的位置信息。让在TextView中显示它
字符串s =;
S + =时间:+ location.getTime()+\ N的;
S + =\ tLatitude:+ location.getLatitude()+\ N的;
S + =\ tLongitude:+ location.getLongitude()+\ N的;
S + =\ tAccuracy:+ location.getAccuracy()+\ N的;
tv.setText(多个);
}
@覆盖
公共无效onProviderDisabled(字符串提供商){}
@覆盖
公共无效onProviderEnabled(字符串提供商){}
@覆盖
公共无效onStatusChanged(字符串商,INT地位,捆绑演员){}
}
在你的主的布局文件(我用的main.xml
):
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT>
<的TextView
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:ID =@ + ID / TextView01
机器人:文本=等待位置信息......
/>
< / LinearLayout中>
然后,在你的的AndroidManifest.xml
:
< XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=org.example.LocationTest
安卓版code =1
机器人:VERSIONNAME =1.0>
<应用
机器人:图标=@可绘制/图标
机器人:标签=@字符串/ APP_NAME>
<活动
机器人:主名称=
机器人:标签=@字符串/ APP_NAME>
<意向滤光器>
<作用机器人:名称=android.intent.action.MAIN/>
<类机器人:名称=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
< /用途>
<使用-SDK安卓的minSdkVersion =3/>
<使用-权限的Android:名称=android.permission.ACCESS_COARSE_LOCATION/>
<使用-权限的Android:名称=android.permission.ACCESS_FINE_LOCATION/>
< /舱单>
您应该能够编译/运行这个就好了。如果用这个,你仍然不能得到修复它不是一个软件问题。要么你有一个坏的GPS或者它只是不能得到任何卫星上的锁。
您可以在这里下载源。
I am trying to get the GPS location of my G1 using the following code
In Activity
MyLocationListener myListener = new MyLocationListener();
LocationManager myManager = (LocationManager)getSystemService(LOCATION_SERVICE);
myManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 0, myListener);
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;
}
}
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.
Adding log
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
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.
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.
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.
UPDATE:
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
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() + "\n";
s += "\tLatitude: " + location.getLatitude() + "\n";
s += "\tLongitude: " + location.getLongitude() + "\n";
s += "\tAccuracy: " + location.getAccuracy() + "\n";
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) {}
}
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>
Then, in your 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>
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.
You can download the source here.
这篇关于Android的 - 使用LocationManager不给地缘修复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!