运行 特定时间的脚本
running an script only for an specific time
我有一个检测位置的代码,我只想工作 2 分钟。
当我触发 start() 方法脚本时,脚本必须运行将近 2 分钟。
问题在于 运行 我的脚本只针对特定时间。
我使用了这段代码,但 运行 不正确。
不要从 Timer().schedule()
中触发 stop() 方法
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) {
}
}
我终于通过使用处理程序解决了我的问题。
阅读此页:Using Bundle Android to Exchange Data Between Threads
a.java
public class a implements LocationListener{
private LocationManager locationManager;
private String provider;
private Location lastloc;
private Context _context;
private Thread workingthread = null;
final Handler mHandler = new Handler(){
public void handleMessage(Message msg) {
Log.d("states","return msg from timer2min");
if(msg.what==1){
stop();
}
super.handleMessage(msg);
}
};
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);
workingthread=new timer2min(mHandler);
workingthread.start();
}
public void stop(){
Log.d("states","stop");
locationManager.removeUpdates((LocationListener) this);
}
@Override
public void onLocationChanged(Location location) {
Log.d("states", "onLocationChanged()");
}
@Override
public void onProviderDisabled(String arg0) {
}
@Override
public void onProviderEnabled(String arg0) {
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
}
timer2min.java
public class timer2min extends Thread {
private Handler hd;
public timer2min(Handler msgHandler){
hd = msgHandler;
}
public void run() {
try {
Thread.sleep(2*60*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Message msg = hd.obtainMessage();
msg.what = 1;
hd.sendMessage(msg);
}
}
再次检查您的 Timer
代码。通常,Timer
代码应该是这样的:
Timer.schedule(TimerTask,
delayTime); // delay a task before executed for the first time, in milliseconds
因为你的 delayTime
是使用 System.currentTimeMillis()
执行的,所以 System
会选择从午夜开始的当前时间(以毫秒为单位),这样 TimerTask
就会被执行百万毫秒后。
因此,使用此代码:
Timer timer = new Timer();
timer.schedule(new TimerTask(){
@Override
public void run() {
// do your thing here
}
}, 2*60*1000);
查看此 documentation 关于您创建的 Timer
的类型。
我有一个检测位置的代码,我只想工作 2 分钟。
当我触发 start() 方法脚本时,脚本必须运行将近 2 分钟。
问题在于 运行 我的脚本只针对特定时间。 我使用了这段代码,但 运行 不正确。
不要从 Timer().schedule()
中触发 stop() 方法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) {
}
}
我终于通过使用处理程序解决了我的问题。
阅读此页:Using Bundle Android to Exchange Data Between Threads
a.java
public class a implements LocationListener{
private LocationManager locationManager;
private String provider;
private Location lastloc;
private Context _context;
private Thread workingthread = null;
final Handler mHandler = new Handler(){
public void handleMessage(Message msg) {
Log.d("states","return msg from timer2min");
if(msg.what==1){
stop();
}
super.handleMessage(msg);
}
};
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);
workingthread=new timer2min(mHandler);
workingthread.start();
}
public void stop(){
Log.d("states","stop");
locationManager.removeUpdates((LocationListener) this);
}
@Override
public void onLocationChanged(Location location) {
Log.d("states", "onLocationChanged()");
}
@Override
public void onProviderDisabled(String arg0) {
}
@Override
public void onProviderEnabled(String arg0) {
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
}
timer2min.java
public class timer2min extends Thread {
private Handler hd;
public timer2min(Handler msgHandler){
hd = msgHandler;
}
public void run() {
try {
Thread.sleep(2*60*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Message msg = hd.obtainMessage();
msg.what = 1;
hd.sendMessage(msg);
}
}
再次检查您的 Timer
代码。通常,Timer
代码应该是这样的:
Timer.schedule(TimerTask,
delayTime); // delay a task before executed for the first time, in milliseconds
因为你的 delayTime
是使用 System.currentTimeMillis()
执行的,所以 System
会选择从午夜开始的当前时间(以毫秒为单位),这样 TimerTask
就会被执行百万毫秒后。
因此,使用此代码:
Timer timer = new Timer();
timer.schedule(new TimerTask(){
@Override
public void run() {
// do your thing here
}
}, 2*60*1000);
查看此 documentation 关于您创建的 Timer
的类型。