Android - 使用LocationManager不提供地理位置修复

los*_*sit 3 gps android

我正在尝试使用以下代码获取G1的GPS位置

在活动中

MyLocationListener myListener = new MyLocationListener();
LocationManager myManager = (LocationManager)getSystemService(LOCATION_SERVICE);
myManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 0, myListener);
Run Code Online (Sandbox Code Playgroud)

这是LocationListener类

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;
  }

}
Run Code Online (Sandbox Code Playgroud)

我等了30秒,但没有一个监听器方法被调用.GPS图标显示,停留一段时间但由于某种原因我甚至没有得到修复,即使在户外在明亮的阳光下.我正在使用1.5 SDK测试G1.

有人可以告诉我代码有什么问题吗?谢谢.

添加日志

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
Run Code Online (Sandbox Code Playgroud)

Jer*_*gan 13

首先,在您的活动中,您正在呼叫的地方requestLocationUpdates(),第二个参数是最短时间,而不是预定时间.您目前要求每2秒更新一次.它实际上不会报告任何东西,直到它可以...有时它需要一点时间.

如果您的GPS在其他地方没有响应/缓慢(例如在地图中),请尝试重新启动手机(有些人声称您必须关闭手机然后拔出电池以完全断开GPS收音机的电源).希望这会使它恢复到正常的响应状态.

此外,它应该没关系,但你说你使用的是1.5 SDK,但你没有提到你是否正在编译它(而不是1.1).我只是提起这个因为GPS在Cupcake中工作得更好/更快,所以我想知道你的手机实际上在运行什么.

更新:

我自己写了一个小测试,看看我是否可以复制你的问题.它只包含三个用户生成的文件,我使用SDK 1.1和1.5进行了测试

在主Activity(我简单使用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) {}
}
Run Code Online (Sandbox Code Playgroud)

在您的"主"布局文件中(我使用过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>
Run Code Online (Sandbox Code Playgroud)

然后,在你的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> 
Run Code Online (Sandbox Code Playgroud)

你应该能够编译/运行它就好了.如果使用这个,你仍然无法修复它不是一个软件问题.您要么GPS不好,要么无法锁定任何卫星.

您可以在此处下载源代码.

  • 谢谢.我已经等了30秒才看到通知栏上的GPS图标而没有真正得到修复!我正在编译并运行1.5. (2认同)