0
我想每5分鐘插入一次SQLite的位置,但是每當它發生變化時插入位置,我是新的,我知道我在onlocationchange
內有插入,但它應該每x次調用一次。我不知道該怎麼辦每隔5分鐘插入一次SQLite的位置
public class GPS extends Service {
public static final int notify = 1000*60*5; //interval between two services(Here Service run every 5 Minute)
private Handler mHandler = new Handler();
private Timer mTimer = null;
private LocationManager locationMangaer = null;
private LocationListener locationListener = null;
private static final String TAG = "Debug";
private Boolean flag = false;
@Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
if (mTimer != null)
mTimer.cancel();
else {
mTimer = new Timer();
mTimer.scheduleAtFixedRate(new TimeDisplay(), 0, notify);
}
locationMangaer = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
}
private Boolean displayGpsStatus() {
ContentResolver contentResolver = getBaseContext().getContentResolver();
boolean gpsStatus = Settings.Secure.isLocationProviderEnabled(contentResolver,LocationManager.GPS_PROVIDER);
if (gpsStatus) {
return true;
} else {
return false;
}
}
public class MyLocationListener implements LocationListener {
dbISMLock dbismlock = new dbISMLock(getBaseContext());
final SQLiteDatabase db =dbismlock.getWritableDatabase();
@Override
public void onLocationChanged(Location loc) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentDateandTime = sdf.format(new Date());
Toast.makeText(getBaseContext(),"fecha: "+ currentDateandTime +"Location changed : Lat: " + loc.getLatitude()+ " Lng: " + loc.getLongitude(),Toast.LENGTH_SHORT).show();
String longitude = "Longitude: " +loc.getLongitude();
Log.v(TAG, longitude);
String latitude = "Latitude: " +loc.getLatitude();
Log.v(TAG, latitude);
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String phoneID = telephonyManager.getDeviceId();
//INSERT
db.execSQL("INSERT INTO GEOLOCATION(phoneId,Fecha,longitude, latitude) VALUES('"+phoneID+"','"+currentDateandTime+"','"+longitude+"','"+latitude+"')");
Log.d("insertamos "," geolocation");
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
@Override
public void onDestroy() {
super.onDestroy();
mTimer.cancel();
Toast.makeText(this, "Service is Destroyed", Toast.LENGTH_SHORT).show();
}
class TimeDisplay extends TimerTask {
@Override
public void run() {
mHandler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(GPS.this, coordenadas(), Toast.LENGTH_SHORT).show();
flag = displayGpsStatus();
if (flag) {
Log.v(TAG, "onClick");
locationListener = new MyLocationListener();
if (ActivityCompat.checkSelfPermission(GPS.this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(GPS.this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
locationMangaer.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, locationListener);
} else {
Log.d("Gps Status!!", "Your GPS is: OFF");
}
}
});
}
}
}
也許你應該寫你自己的服務來完成這項工作 - 每隔5分鐘保存一次位置 –
M ..每當位置發生變化時,插入位置的功能是什麼,但是如何從中獲得經度和緯度計時器? –
我的問題是我有我需要的東西,但不是在我想要的時間範圍內。 –