Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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");
- }
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement