您好我正在編寫程序,它使用Alarmmanager每隔5分鐘獲取用戶當前的經度和經度,並將其存儲在共享偏好設置中。我想運行此搜索背景,以便使用service.I要停止服務獲取的lattitude和經度,以避免電池drain.This後是我的代碼執行後停止服務
CurrentLocation.java
import com.google.android.gms.maps.model.LatLng;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;
public class CurrentLocation extends Service implements LocationListener {
private Context mcontext;
SharedPreferences shprefs;
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
this.mcontext=this;
getlocation();
}
private void getlocation() {
// TODO Auto-generated method stub
Log.i("evvterer", "mnbj");
LocationManager locationManager = (LocationManager) mcontext.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
shprefs=PreferenceManager.getDefaultSharedPreferences(CurrentLocation.this);
double latitude=location.getLatitude();
Editor edit=shprefs.edit();
edit.putString("Ltd", ""+latitude);
edit.commit();
double longitude=location.getLongitude();
Editor edits=shprefs.edit();
edits.putString("Lngtd", ""+longitude);
edits.commit();
LatLng loca=new LatLng(latitude,longitude);
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
請幫我 在此先感謝