【发布时间】:2016-09-25 04:21:56
【问题描述】:
嘿,我正在为我的最终测试创建一个 android 汽车跟踪应用程序,但是当我测试我的跟踪应用程序时,它不断向我的数据库发送相同的坐标这是我的 gpstracking 代码
package com.example.id.library;
import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
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.util.Log;
public class gps extends Service implements LocationListener {
private final Context mContext;
// flag buat status gps. on atau nda
boolean isGPSEnabled = false;
// flag buat network.
boolean isNetworkEnabled = false;
// flag buat posisi gps. bisa dapat koordinat atau nda
boolean canGetLocation = false;
Location location; // variabel untuk lokasi
double latitude; // latitudenya
double longitude; // longitudenya
// minimum perbedaan jarak untuk update
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meter
// minimum selang waktu tiap update lokasi
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 menit
// deklarasi Location Manager
protected LocationManager locationManager;
public gps(Context context) {
this.mContext = context;
getLocation();
}
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
// tarik status gps
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// tarik status network
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// kalo semua GPS dan Network mati
// buka alert untuk enable gps
} else { // kalo GPS atau network bisa dapat lokasi
this.canGetLocation = true;
// ambil lokasi dari network dulu
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// kalo GPS ON, ambil koordinat yang lebih presisi
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
// fungsi untuk stop listener GPS
public void stopUsingGPS() {
if (locationManager != null) {
locationManager.removeUpdates(gps.this);
}
}
// fungsi untuk ambil latitude
public double getLatitude() {
if (location != null) {
latitude = location.getLatitude();
}
// return nilai latitude
return latitude;
}
// fungsi untuk ambil longitude
public double getLongitude() {
if (location != null) {
longitude = location.getLongitude();
}
// return nilai longitude
return longitude;
}
// fungsi untuk cek gps bisa ambil koordinat atau nda
public boolean canGetLocation() {
return this.canGetLocation;
}
// fungsi untuk alert dialog kalau GPS mati
public void showSettingsAlert() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
// title
alertDialog.setTitle("Loading GPS");
// Dialog
alertDialog
.setMessage("GPS dalam kondisi mati. Silakan nyalakan GPS terlebih dahulu.");
// tombol setting
alertDialog.setPositiveButton("Settings",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
// tombol cancel
alertDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// tampilkan alert
alertDialog.show();
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}
这里是我的 gpsracking 发送到我的数据库的坐标
请有人帮助我,对不起我的英语不好
【问题讨论】:
标签: android eclipse gps tracking