Android GPS不再提供更新

时间:2015-05-20 19:57:19

标签: android gps

我自己的Android应用程序不再向我提供GPS位置更新。我试图建立最简单的应用程序,它仍然无法正常工作。状态栏中有一个闪烁的GPS图标(因此无法关闭GPS吗?),但是我没有获得locationChanged更新。

我完全不知道问题是什么。

清单包括:

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

Mainactivity.java

public class MainActivity extends ActionBarActivity {
    private LocationManager locationManager;
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.textView);
        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    }

    LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
                textView.setText(location.getLatitude() + "  " + location.getLongitude());
        }
        public void onStatusChanged(String provider, int status, Bundle extras) {        }
        public void onProviderEnabled(String provider) {        }
        public void onProviderDisabled(String provider) {        }
    };
}

1 个答案:

答案 0 :(得分:0)

更改活动以实现LocationListener。

public class MainActivity extends ActionBarActivity implements LocationListener{

从内部类中取出函数,将locationListener更改为此。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView = (TextView) findViewById(R.id.textView);
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0
         , this); //note changed last parameter to this.
}
//The following methods were inside the inner class.  They were not created in time 
//for oncreate to requestLocationUpdates.
public void onLocationChanged(Location location) {
            textView.setText(location.getLatitude() + "  " 
            + location.getLongitude());
    }
public void onStatusChanged(String provider, int status, Bundle extras) {        }
public void onProviderEnabled(String provider) {        }
public void onProviderDisabled(String provider) {        }

试一试,看看它是否效果更好。 Activity本身现在是一个LocationListener,更新将来到这个类。