Android GPS 不再提供更新
Android GPS isn't providing updates anymore
我自己的 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) { }
};
}
更改 Activity 以同时实现 LocationListener。
public class MainActivity extends ActionBarActivity implements LocationListener{
从内部class取出函数,把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,更新将来到这个 class。
我自己的 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) { }
};
}
更改 Activity 以同时实现 LocationListener。
public class MainActivity extends ActionBarActivity implements LocationListener{
从内部class取出函数,把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,更新将来到这个 class。