【发布时间】:2019-02-06 09:40:06
【问题描述】:
我试图在 gps 被禁用时弹出一个窗口消息,但在我启用或禁用位置侦听器时没有响应。我不明白我在做什么错。任何人都可以帮我解决这个问题吗?提前致谢!
这是我使用 locationlistener 的代码:
public class MapsActivityy extends AppCompatActivity {
private LocationManager locationManager;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
//locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Initialize location manager.
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Check if user has granted permissions.
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// We do not have permissions.
return;
}
// Check if we did find a location provider.
if (locationManager == null) {
// No gps provider was found. Inform user.
return;
}
// Initialize location manager and register location listener.
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
2000,
10, locationListenerGPS);
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
openGpsWindow();
}else{
updateLocationUI();
}
}
//Location listener that listens for location state changes.
LocationListener locationListenerGPS = new LocationListener() {
@Override
public void onLocationChanged(android.location.Location location) {
// Called when the location has changed.
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// Called when the provider status changes.
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
// Called when the provider is disabled by the user.
openGpsWindow();
}
};
//Pops up a window in order to open GPS
public void openGpsWindow() {
Intent intent = new Intent(this, EnableGpsWindow.class);
startActivity(intent);
}
}
我试图在 gps 被禁用时弹出一个窗口消息,但在我启用或禁用位置侦听器时没有响应。我不明白我在做什么错。任何人都可以帮我解决这个问题吗?提前致谢!
【问题讨论】: