我想确保如果用户在应用程序启动时关闭GPS位置,则会出现一个弹出窗口,并将您重定向到可以切换GPS位置的位置。 我能够做到这一点,但一旦我回到我的应用程序,它不再在我的位置居中,除非我关闭应用程序并再次启动它。 我做错了什么?
private boolean hasPermissions(){
return ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED;
}
在onCreate中:
if (hasPermissions()) {
setMap();
initMyLocation();
centerButton();
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
}
setMap,initMyLocation,centerButton方法:
public void setMap(){
map = findViewById(R.id.mapview);
map.setTileSource(TileSourceFactory.MAPNIK);
map.setTilesScaledToDpi(true);
map.setBuiltInZoomControls(true);
map.setMultiTouchControls(true);
}
public void initMyLocation(){
final LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
if(!lm.isProviderEnabled(LocationManager.GPS_PROVIDER) || !lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
//Build the alert dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Something.");
builder.setMessage("activate GPS.");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
});
Dialog alertDialog = builder.create();
alertDialog.setCanceledOnTouchOutside(false);
alertDialog.show();
}
GpsMyLocationProvider provider = new GpsMyLocationProvider(getApplicationContext());
provider.addLocationSource(LocationManager.GPS_PROVIDER);
provider.addLocationSource(LocationManager.NETWORK_PROVIDER);
locationNewOverlay = new MyLocationNewOverlay(provider, map);
locationNewOverlay.enableMyLocation();
map.getOverlays().add(locationNewOverlay);
}
public void centerButton() {
ImageButton btCenterMap = this.findViewById(R.id.menu_mylocation);
btCenterMap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
locationNewOverlay.runOnFirstFix(new Runnable() {
@Override
public void run() {
myLocation = locationNewOverlay.getMyLocation();
}
});
locationNewOverlay.enableFollowLocation();
}
});
}
答案 0 :(得分:0)
我认为你的逻辑必须是这样的:
检查方法 - 检查权限以及GPS /网络位置是否可用
简历
暂停时