【问题标题】:insert into SQLite the location every 5 minutes每 5 分钟将位置插入 SQLite
【发布时间】:2017-02-17 13:47:35
【问题描述】:

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

            }
        });
    }
}

}

【问题讨论】:

  • 或许您应该编写自己的服务来完成这项工作 - 每 5 分钟保存一次位置
  • M .. 它的作用是在每次位置更改时插入位置,但是我如何从计时器中获取纬度和经度?
  • 我的问题是我有我需要的东西,但不是在我想要的时间范围内。

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


【解决方案1】:

您可以使用Service 在后台收听位置变化,并每 5 分钟更新一次。 onLocationchange() 用于手动收听更改。因此,您可以忽略它并使用服务。

【讨论】:

  • 我该怎么做?我应该使用 linstener 来获取更改或不更改的位置
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-27
  • 2017-12-06
  • 2021-04-05
  • 1970-01-01
  • 2021-04-21
  • 1970-01-01
相关资源
最近更新 更多