我有一个检测位置的代码,我只想工作2分钟。仅在特定时间运行脚本
当我启动start()方法的脚本必须工作大约2分钟。
问题在那里,如何运行我的脚本只在特定的时间。 我用这个代码但没有正确运行。
不从定时器挡火()()方法。时间表()
public class a implements LocationListener{
private LocationManager locationManager;
private String provider;
private Location lastloc;
private Context _context;
public a(Context context){
_context = context;
}
public void start(){
locationManager = (LocationManager) _context.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1, (LocationListener) this);
new Timer().schedule(
new TimerTask(){
public void run() {
stop();
}
}
,System.currentTimeMillis(), 2*60*1000);
}
public void stop(){
Log.d("states","stop");
locationManager.removeUpdates((LocationListener) this);
}
@Override
public void onLocationChanged(Location location) {
Log.d("states", "onLocationChanged()");
lastloc = location;
}
@Override
public void onProviderDisabled(String arg0) {
}
@Override
public void onProviderEnabled(String arg0) {
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
}
你的代码的行为如何?它会在2分钟后继续更新还是在此之前停止更新? – hasan83 2015-03-25 07:50:23
它在2分钟后不断更新并且不停止。 – saeid 2015-03-25 08:23:28
我相信你的问题是不是在这个代码 – hasan83 2015-03-25 08:48:09