2017-02-17 2 views
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"); 
       } 

      } 
     }); 
    } 
} 

}

+0

아마도 당신은 작업을 수행 할 자신의 서비스를 작성해야합니다 - 매 5 분마다 위치를 저장하십시오. –

+0

M .. 위치가 변경 될 때마다 위치를 삽입하는 것이지만 어떻게하면 위도와 경도를 얻을 수 있습니까? 시간제 노동자? –

+0

내 문제는 내가 필요한 것을 가지고 있지만, 내가 원하는 시간 범위가 아니라는 것이다. –

답변

0

당신은 5 분마다 백그라운드에서 위치 변화를 듣고 그것을 업데이트 할 Service을 사용할 수 있습니다 어떻게 해야할지 모르겠어요. onLocationchange()을 수동으로 듣고을 변경하는 데 사용됩니다. 예를 들어이를 무시하고 서비스를 사용할 수 있습니다.

+0

어떻게 할 수 있습니까? linstener를 사용하여 위치를 변경하거나 변경해야합니다. –

관련 문제