用户启用 GPS 后获取位置更新
Get location updates after GPS enabled by user
我有一个简单的应用程序,目前只要求必要的权限,如果 GPS 关闭,您会收到一个 AlertDialog,询问您是否要将其打开。接受后,转到 GPS 选项,启用它,然后返回到我的应用程序,我想更新位置,但在这里我迷路了。
换句话说,我正在尝试执行此处所述的操作:
不幸的是,我无法完成它,而且这个例子对我来说有点复杂,难以理解。这是我的一段代码,显示了相关位:
private void checkGPS() {
manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.GPS_error)
.setCancelable(false)
.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
Intent gps = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(gps, 1);
getLatLon();
}
})
.setNegativeButton(R.string.deny, new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
} else {
getLatLon();
}
}
private void getLatLon() {
//manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = manager.getBestProvider(criteria, false);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
manager.getLastKnownLocation(provider);
if (location != null) {
Toast.makeText(this, "This is my location: " + location.getLongitude() + ", " + location.getLatitude(), Toast.LENGTH_SHORT).show();
} else {
// manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
//location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
/*
double longitude = location.getLongitude();
double latitude = location.getLatitude();
Toast.makeText(this, "This is my location: " + longitude + ", " + latitude, Toast.LENGTH_SHORT).show();
*/
}
}
}
@Override
public void onLocationChanged(Location l) {
location = l;
double longitude = location.getLongitude();
double latitude = location.getLatitude();
Toast.makeText(this, "This is my location: " + longitude + ", " + latitude, Toast.LENGTH_SHORT).show();
}
在请求 ACCESS_FINE_LOCATION 许可后(清单上也有说明)我调用 checkGPS()。如前所述,让您启用或不启用 GPS。如果启用,我会调用 getLatLon()。如果有 lastKnownLocation,很好,如果没有...
我在这里迷路了。我调用 requestLocationUpdates 然后什么也不做,等待 onLocationChanged 接收位置更新并执行其余代码。我做对了吗?结果是我点击按钮,打开 GPS。再次点击按钮,没有任何反应。
这方面的任何帮助都会有所帮助。
非常感谢您的宝贵时间。
在禁用 GPS 的情况下,您当前的代码不会在调用 getLatLon()
之前等待用户做出选择。
您将需要添加一个 onActivityResult()
覆盖,当用户返回您的应用时将调用该覆盖。
首先,在 checkGPS()
方法中移除对 getLatLon()
的调用,以解决 GPS 被禁用的情况:
private void checkGPS() {
manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.GPS_error)
.setCancelable(false)
.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
Intent gps = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(gps, 1);
//Remove this:
//getLatLon();
}
})
.setNegativeButton(R.string.deny, new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
} else {
getLatLon();
}
}
然后,添加 onActivityResult()
覆盖,再次检查设置,如果现在已启用,则调用 getLatLon()
:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
getLatLon();
}
}
我在这里开发了融合位置 api 演示应用程序和实用程序包。
如果对您有用,请尝试一下。要使用融合位置 api 获取位置,您只需编写以下代码片段...
new LocationHandler(this)
.setLocationListener(new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// Get the best known location
}
}).start();
如果您想对其进行自定义,只需在此处查找文档...
https://github.com/abhishek-tm/general-utilities-android/wiki/Location-Handler
我已经根据您的需要编写了示例代码,这将在内部处理 GPS enable/disable 对话框,试试这个...
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import in.teramatrix.utilities.service.LocationHandler;
import in.teramatrix.utilities.util.MapUtils;
/**
* Lets see how to use utilities module by implementing location listener.
*
* @author Khan
*/
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback, LocationListener {
private GoogleMap map;
private Marker marker;
private LocationHandler locationHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Obtaining an instance of map
FragmentManager manager = getSupportFragmentManager();
SupportMapFragment mapFragment = (SupportMapFragment) manager.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
this.locationHandler = new LocationHandler(this)
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(5000)
.setFastestInterval(10000)
.setLocationListener(this);
}
@Override
public void onMapReady(GoogleMap map) {
this.map = map;
this.locationHandler.start();
}
@Override
public void onLocationChanged(Location location) {
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
if (marker == null) {
marker = MapUtils.addMarker(map, latLng, R.drawable.ic_current_location);
map.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 14), 500, null);
} else {
marker.setPosition(latLng);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (locationHandler != null) {
locationHandler.stop();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == LocationHandler.REQUEST_LOCATION) {
locationHandler.start();
}
}
}
希望对您有所帮助。
忙于其他项目一段时间后,回到这个项目,我删除了 getLatLon();来自 checkGPS() 的函数;功能就是这样,代码很好。我正在使用模拟器检查这是否有效,但我忘记了模拟器有一个固定的纬度和经度值,所以你不会像真正的手机那样得到更新 phone,因此它看起来好像工作不正常。
有点像新手犯的错误。无论如何,感谢您的报价。看看做同一件事的不同方法很有趣。
沙妥昔单抗
我有一个简单的应用程序,目前只要求必要的权限,如果 GPS 关闭,您会收到一个 AlertDialog,询问您是否要将其打开。接受后,转到 GPS 选项,启用它,然后返回到我的应用程序,我想更新位置,但在这里我迷路了。
换句话说,我正在尝试执行此处所述的操作:
不幸的是,我无法完成它,而且这个例子对我来说有点复杂,难以理解。这是我的一段代码,显示了相关位:
private void checkGPS() {
manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.GPS_error)
.setCancelable(false)
.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
Intent gps = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(gps, 1);
getLatLon();
}
})
.setNegativeButton(R.string.deny, new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
} else {
getLatLon();
}
}
private void getLatLon() {
//manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = manager.getBestProvider(criteria, false);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
manager.getLastKnownLocation(provider);
if (location != null) {
Toast.makeText(this, "This is my location: " + location.getLongitude() + ", " + location.getLatitude(), Toast.LENGTH_SHORT).show();
} else {
// manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
//location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
/*
double longitude = location.getLongitude();
double latitude = location.getLatitude();
Toast.makeText(this, "This is my location: " + longitude + ", " + latitude, Toast.LENGTH_SHORT).show();
*/
}
}
}
@Override
public void onLocationChanged(Location l) {
location = l;
double longitude = location.getLongitude();
double latitude = location.getLatitude();
Toast.makeText(this, "This is my location: " + longitude + ", " + latitude, Toast.LENGTH_SHORT).show();
}
在请求 ACCESS_FINE_LOCATION 许可后(清单上也有说明)我调用 checkGPS()。如前所述,让您启用或不启用 GPS。如果启用,我会调用 getLatLon()。如果有 lastKnownLocation,很好,如果没有...
我在这里迷路了。我调用 requestLocationUpdates 然后什么也不做,等待 onLocationChanged 接收位置更新并执行其余代码。我做对了吗?结果是我点击按钮,打开 GPS。再次点击按钮,没有任何反应。
这方面的任何帮助都会有所帮助。 非常感谢您的宝贵时间。
在禁用 GPS 的情况下,您当前的代码不会在调用 getLatLon()
之前等待用户做出选择。
您将需要添加一个 onActivityResult()
覆盖,当用户返回您的应用时将调用该覆盖。
首先,在 checkGPS()
方法中移除对 getLatLon()
的调用,以解决 GPS 被禁用的情况:
private void checkGPS() {
manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.GPS_error)
.setCancelable(false)
.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
Intent gps = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(gps, 1);
//Remove this:
//getLatLon();
}
})
.setNegativeButton(R.string.deny, new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
} else {
getLatLon();
}
}
然后,添加 onActivityResult()
覆盖,再次检查设置,如果现在已启用,则调用 getLatLon()
:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
getLatLon();
}
}
我在这里开发了融合位置 api 演示应用程序和实用程序包。
如果对您有用,请尝试一下。要使用融合位置 api 获取位置,您只需编写以下代码片段...
new LocationHandler(this)
.setLocationListener(new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// Get the best known location
}
}).start();
如果您想对其进行自定义,只需在此处查找文档...
https://github.com/abhishek-tm/general-utilities-android/wiki/Location-Handler
我已经根据您的需要编写了示例代码,这将在内部处理 GPS enable/disable 对话框,试试这个...
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import in.teramatrix.utilities.service.LocationHandler;
import in.teramatrix.utilities.util.MapUtils;
/**
* Lets see how to use utilities module by implementing location listener.
*
* @author Khan
*/
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback, LocationListener {
private GoogleMap map;
private Marker marker;
private LocationHandler locationHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Obtaining an instance of map
FragmentManager manager = getSupportFragmentManager();
SupportMapFragment mapFragment = (SupportMapFragment) manager.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
this.locationHandler = new LocationHandler(this)
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(5000)
.setFastestInterval(10000)
.setLocationListener(this);
}
@Override
public void onMapReady(GoogleMap map) {
this.map = map;
this.locationHandler.start();
}
@Override
public void onLocationChanged(Location location) {
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
if (marker == null) {
marker = MapUtils.addMarker(map, latLng, R.drawable.ic_current_location);
map.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 14), 500, null);
} else {
marker.setPosition(latLng);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (locationHandler != null) {
locationHandler.stop();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == LocationHandler.REQUEST_LOCATION) {
locationHandler.start();
}
}
}
希望对您有所帮助。
忙于其他项目一段时间后,回到这个项目,我删除了 getLatLon();来自 checkGPS() 的函数;功能就是这样,代码很好。我正在使用模拟器检查这是否有效,但我忘记了模拟器有一个固定的纬度和经度值,所以你不会像真正的手机那样得到更新 phone,因此它看起来好像工作不正常。
有点像新手犯的错误。无论如何,感谢您的报价。看看做同一件事的不同方法很有趣。
沙妥昔单抗