【发布时间】:2018-07-02 10:48:45
【问题描述】:
我是 Android 新手,根据位置创建一个简单的应用程序。 当用户在不打开位置的情况下打开应用程序时,它将直接进入设置,如果他们打开则从那里进入 获取 lat 和 lang 值为时已晚。
import android.Manifest;
import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
public class GPSTracking extends Service implements LocationListener
{
private final Context mContext;
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
private boolean canGetLocation = false;
Location mLocation;
private double lat;
private double lng;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATE = 10;
private static final long MIN_TIME_BW_UPDATE = 1000 * 60 * 1;
protected LocationManager locationManager;
public GPSTracking(Context context)
{
this.mContext = context;
getLocation();
}
public Location getLocation()
{
try
{
locationManager = (LocationManager)
mContext.getSystemService(LOCATION_SERVICE);
isGPSEnabled =
locationManager.isProviderEnabled(lo
cationManager.GPS_PROVIDER);
isNetworkEnabled =
locationManager.isProviderEnabled(locat
ionManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled)
{
//no network or gps
}
else
{
setCanGetLocation(true);
if (isNetworkEnabled)
{
if(locationManager!=null)
{
mLocation =
locationManager.getLastKnownLocation
(LocationManager.NETWORK_PROVIDER);
if(mLocation!=null)
{
setLat(mLocation.getLatitude());
setLng(mLocation.getLongitude());
}
}
}
if (isGPSEnabled)
{
if (mLocation == null)
{
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATE,
MIN_DISTANCE_CHANGE_FOR_UPDATE, this);
if (locationManager != null)
{
mLocation = locationManager
.getLastKnownLocation(LocationManager
.GPS_PROVIDER);
if (mLocation != null)
{
setLat(mLocation.getLatitude());
setLng(mLocation.getLongitude());
}
}
}
}
}
}
catch (Exception e)
{
//Exception
}
return mLocation;
}
public void showAlertDialog()
{
AlertDialog.Builder alertDialog = new
AlertDialog.Builder(mContext);
alertDialog.setTitle("GPS is settings");
alertDialog.setMessage("GPS is not enabled. Do you want go to
settings menu?");
alertDialog.setPositiveButton("Settings", new
DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
Intent intent = new
Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
alertDialog.setNegativeButton("Cancel", new
DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
dialog.cancel();
}
});
alertDialog.show();
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle
extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider)
{
//Toast.makeText(mContext, "Please turn on the location",
// Toast.LENGTH_SHORT).show();
}
public double getLat() {
return lat;
}
public void setLat(double lat) {
this.lat = lat;
}
public double getLng() {
return lng;
}
public void setLng(double lng) {
this.lng = lng; //this is the lat
}
public boolean isCanGetLocation() {
return canGetLocation;
}
public void setCanGetLocation(boolean canGetLocation)
{
this.canGetLocation = canGetLocation; //this is canGetLocation which will do the following thing.
}
}`*
【问题讨论】:
-
很遗憾,您选择了错误的示例代码。这种方法通常被称为“GPSTracker”,它有一些问题,例如它
extends Service,但实际上并没有真正用作服务,它请求位置更新,但不在onLocationChanged()中处理它们.不过它可能工作得很好。居然有人写了a detailed blog post about the problems in that code。 -
哦,知道了。非常感谢。
标签: android android-gps