大家好,这是我的服务类,我用它来获取位置,即使我在后台
public class MyService extends Service {
private static final String TAG = MyService.class.getName();
private LocationManager mLocationManager = null;
private static final int LOCATION_INTERVAL = /*600000*/1000;
private static final float LOCATION_DISTANCE = 0;
public class BackgroundLocation implements LocationListener {
private final String LOG_TAG = TrackingLocation.class.getName();
Context context;
LocationManager locationManager;
public Location currentLocation;
private Tracker tracker;
long userId;
long projectId;
long savedTime;
IProjectController iProjectController;
HttpCookie authCookie;
IAuthPref_ iAuthPref;
RestAdapter adapter;
AlertDialog gpsDisabledAlert;
SlumviewPersistence persistence;
public BackgroundLocation(final Context context, long userID, long projectID) {
this.context = context;
this.userId = userID;
this.projectId = projectID;
iAuthPref = new IAuthPref_(this.context);
if (locationManager == null)
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
//return TODO;
}
if (isGpsEnabled()) {
Log.i(LOG_TAG, "GPS");
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 6000, 0, this);
CountDownTimer timer = new CountDownTimer(10000, 10000) {
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
if (currentLocation != null && currentLocation.getLatitude() != 0.00 && currentLocation.getLongitude() != 0.00) {
} else {
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// public void requestPermissions(@NonNull String[] permissions, int requestCode)
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for Activity#requestPermissions for more details.
return;
}
locationManager.removeUpdates(BackgroundLocation.this);
Log.i(LOG_TAG, "Switching");
switchToNP();
}
}
}.start();
} else {
alertToEnableGPS();
}
}
private void switchToNP() {
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// public void requestPermissions(@NonNull String[] permissions, int requestCode)
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for Activity#requestPermissions for more details.
return;
}
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 6000, 0, this);
Log.i(LOG_TAG, "NP");
}
public void alertToEnableGPS() {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Your Device's GPS is Disable")
.setCancelable(false)
.setTitle("GPS Error")
.setPositiveButton("Gps On",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
context.startActivity(myIntent);
dialog.cancel();
}
});
gpsDisabledAlert = builder.create();
gpsDisabledAlert.show();
}
public Boolean isGpsEnabled() {
ContentResolver contentResolver = context.getContentResolver();
boolean gpsStatus = Settings.Secure
.isLocationProviderEnabled(contentResolver, LocationManager.GPS_PROVIDER);
if (gpsStatus) {
return true;
} else {
return false;
}
}
@Override
public void onLocationChanged(Location location) {
long curTime = System.currentTimeMillis();
Log.i("GetLocation", "Location " + location + " currtie: " + curTime + " saved: " + savedTime);
if (curTime > savedTime + 10 * 60 * 1000) { //this should hopefully cause the update interval to be 10 mins.
if (location != null && location.getLatitude() != 0.0 && location.getLongitude() != 0.0) {
currentLocation = location;
}
}
private void showConnectionLossMessage() {
Toast.makeText(context, "Sorry no connection available. Please try again after sometime", Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "onStartCommand");
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
@Override
public void onCreate() {
Log.i(TAG, "onCreate");
BackgroundLocation location = new BackgroundLocation(this, userid, projectid);
}
@Override
public void onDestroy() {
Log.i(TAG, "onDestroy");
super.onDestroy();
/*if (mLocationManager != null) {
for (int i = 0; i < mLocationListeners.length; i++) {
try {
mLocationManager.removeUpdates(mLocationListeners[i]);
} catch (Exception ex) {
Log.i(TAG, "fail to remove location listners, ignore", ex);
}
}
}*/
}
}
我也是第一次使用服务。 我想错过了一些重要的事情。任何人都可以指出它是什么?