每5分钟插入一次SQLite位置

时间:2017-02-17 13:47:35

标签: java android android-studio geolocation android-sqlite

我想每隔5分钟向SQLite插入一个位置,但每次更改时都会插入位置,我是新手,我知道我在onlocationchange内有插入,但它应该是每x次拨打电话。我不知道该怎么做

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");
                }

            }
        });
    }
}

}

1 个答案:

答案 0 :(得分:0)

您可以使用Service在后​​台收听位置更改,并每隔5分钟更新一次。 onLocationchange()用于监听手动更改。所以你可以忽略它,例如使用服务。