我想在获取位置后删除locationListener。我正在使用相同的listenerOBj我请求位置更新,但它无法正常工作。我有另一个问题,通过调用serviceClassObject.stopself没有停止服务?
提前致谢。
这是我的代码
public class GPSTracker extends Service implements LocationListener {
private final Context mContext;
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetLocation = false;
Location location; // location
double latitude; // latitude
double longitude; // longitude
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
protected LocationManager locationManagerGPs,locationManagerNet;
public GPSTracker(Context context) {
this.mContext = context;
getLocation();
}
public Location getLocation() {
try {
locationManagerGPs = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
locationManagerNet = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManagerGPs
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManagerNet
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
locationManagerGPs.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
locationManagerNet.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (isGPSEnabled || isNetworkEnabled) {
this.canGetLocation = true;
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (locationManagerGPs != null) {
location = locationManagerGPs
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
Log.d("tttt","GPStracker taken complete");
}
}
}
else{
if (locationManagerNet != null) {
location = locationManagerNet
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
Log.d("tttt","network taken complete");
}
}
}
}else{
this.canGetLocation = false;
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
public double getLatitude(){
if(location != null){
latitude = location.getLatitude();
}
return latitude;
}
public double getLongitude(){
if(location != null){
longitude = location.getLongitude();
}
return longitude;
}
public void stopUsingGPS(){
if(locationManagerGPs != null){
Log.d("tttt","trying to stop GPStracker");
locationManagerGPs.removeUpdates(this);
}
if(locationManagerNet != null){
Log.d("tttt","trying to stop GPStracker net");
locationManagerNet.removeUpdates(this);
}
}
public boolean canGetLocation() {
return this.canGetLocation;
}
@Override
public void onLocationChanged(Location location) {
Log.d("tttt","onLocationChanged called on GPStracker");
getLocation();
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
Log.d("tttt","location provider is enabled");
getLocation();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("tttt","onStatusChanged called on GPStracker");
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
我从活动中打来电话
GPSTracker gps;
gps = new GPSTracker(HomeActivity.this);
if(gps.canGetLocation()){
lat = gps.getLatitude();
lng = gps.getLongitude();
gps.stopUsingGPS();
gps.stopSelf();
}
答案 0 :(得分:0)
最后,我终于得到了解决方案。主要问题在于使用服务的方法。仅通过扩展服务类不是使用服务的正确方法。当我们需要在服务的帮助下完成某项任务时,必须遵循一些步骤。
步骤1:在扩展服务类之后,我们需要覆盖onStartCommand(Intent intent,int flags,int startId)和onDestroy()方法。
第2步:将其添加到清单中,如
步骤3:启动服务写入startService(new Intent(getBaseContext(),className.class));
步骤4:任务完成后,将其停止为stopService(new Intent(getBaseContext(),className.class));