如何在 android 30 秒后获取位置?
How can I get location after 30 seconds in android?
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.widget.Toast;
public class LocationService extends Service {
private LocationDatabaseHelper mLocationDatabaseHelper;
private LocationModel mLocationModel;
private Date mDate;
private Handler mHandler = new Handler();
private Timer mTimer = null;
private int mCount = 0;
public static final long NOTIFY_INTERVAL = 30 * 1000;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
// cancel if already existed
if (mTimer != null) {
mTimer.cancel();
} else {
// recreate new
mTimer = new Timer();
}
mLocationModel = LocationModel.getInstance();
// schedule task
mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 0, NOTIFY_INTERVAL);
}
@Override
public void onDestroy() {
mTimer.cancel();
}
private class TimeDisplayTimerTask extends TimerTask implements LocationListener {
@Override
public void run() {
mHandler.post(new Runnable() {
@Override
public void run() {
//I send message to draw map here
sendMessage();
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
TimeDisplayTimerTask.this);
}
});
}
@Override
public void onLocationChanged(Location location) {
// I get location and do work here
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
}
private void sendMessage() {
Intent intent = new Intent("my-event");
intent.putExtra("message", "data");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
}
我想要的是每 30 秒获取一次用户位置,但此代码无法按预期工作。它获取位置的速度非常快(我想是每秒)。
我尝试以这种方式获取位置,因为它可以在我开始后立即获取我的当前位置 app.I 之前尝试过 getLastKnowLocation,但它给了我最后一个已知位置,它离我所在的位置很远.
请告诉我如何修复 this.Thank 你!
尝试使用
TimerTask scanTask;
final Handler handler = new Handler();
mTimer = new Timer();
public void sendSMS(){
scanTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
//your method here which you want to call every 30 sec
}
});
}};
mTimer.schedule(scanTask, 30000, 30000);
}
在requestLocationUpdates方法中第二个参数是位置更新的最小时间间隔,以毫秒为单位,所以你只需要这样做:
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30 * 1000, 0, TimeDisplayTimerTask.this);
根据Android开发人员参考文档
public void requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener)
注册位置更新时,LocationManager
使用最新的 Location
对象调用 LocationListener
onLocationChanged(Location)
方法。
而requestLocationUpdates
方法的第二个参数是
minTime
位置更新之间的最小时间间隔,以毫秒为单位
这并不意味着您将每 30 秒不断获得位置更新,因为如果无法获得位置,您也不会获得更新,如果位置不被更改,您将再次无法获得任何更新。
无论如何,如果您想每 30 秒不断更新位置,您可以保留最新位置并使用您的调度程序发送它,同时在调用 onLocationChanged
方法时更新它。
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.widget.Toast;
public class LocationService extends Service {
private LocationDatabaseHelper mLocationDatabaseHelper;
private LocationModel mLocationModel;
private Date mDate;
private Handler mHandler = new Handler();
private Timer mTimer = null;
private int mCount = 0;
public static final long NOTIFY_INTERVAL = 30 * 1000;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
// cancel if already existed
if (mTimer != null) {
mTimer.cancel();
} else {
// recreate new
mTimer = new Timer();
}
mLocationModel = LocationModel.getInstance();
// schedule task
mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 0, NOTIFY_INTERVAL);
}
@Override
public void onDestroy() {
mTimer.cancel();
}
private class TimeDisplayTimerTask extends TimerTask implements LocationListener {
@Override
public void run() {
mHandler.post(new Runnable() {
@Override
public void run() {
//I send message to draw map here
sendMessage();
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
TimeDisplayTimerTask.this);
}
});
}
@Override
public void onLocationChanged(Location location) {
// I get location and do work here
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
}
private void sendMessage() {
Intent intent = new Intent("my-event");
intent.putExtra("message", "data");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
}
我想要的是每 30 秒获取一次用户位置,但此代码无法按预期工作。它获取位置的速度非常快(我想是每秒)。
我尝试以这种方式获取位置,因为它可以在我开始后立即获取我的当前位置 app.I 之前尝试过 getLastKnowLocation,但它给了我最后一个已知位置,它离我所在的位置很远.
请告诉我如何修复 this.Thank 你!
尝试使用
TimerTask scanTask;
final Handler handler = new Handler();
mTimer = new Timer();
public void sendSMS(){
scanTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
//your method here which you want to call every 30 sec
}
});
}};
mTimer.schedule(scanTask, 30000, 30000);
}
在requestLocationUpdates方法中第二个参数是位置更新的最小时间间隔,以毫秒为单位,所以你只需要这样做:
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30 * 1000, 0, TimeDisplayTimerTask.this);
根据Android开发人员参考文档
public void requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener)
注册位置更新时,LocationManager
使用最新的 Location
对象调用 LocationListener
onLocationChanged(Location)
方法。
而requestLocationUpdates
方法的第二个参数是
minTime
位置更新之间的最小时间间隔,以毫秒为单位
这并不意味着您将每 30 秒不断获得位置更新,因为如果无法获得位置,您也不会获得更新,如果位置不被更改,您将再次无法获得任何更新。
无论如何,如果您想每 30 秒不断更新位置,您可以保留最新位置并使用您的调度程序发送它,同时在调用 onLocationChanged
方法时更新它。