您好我有一个计时器任务,每1分钟检查一次文件
public class MyTimerTask extends TimerTask {
//java.io.File file = new java.io.File("/mnt/sdcard/Bluetooth/1.txt");
java.io.File file = new java.io.File("init.rc");
public void CheckTheFile()
{
if (file.exists())
{
// I want here to start the Activity GetGPS
}
}
@Override
public void run() {
CheckTheFile();
}
}
在file.exists的检查中,我想如果文件在那里,活动名为GetGPS。
感谢。
答案 0 :(得分:1)
在你的情况下,我建议使用Handler类。这就是我要做的事情:
private static class PromoScroller implements Runnable {
private Handler _scrollHandler;
public PromoScroller(Handler scrollHandler) {
_scrollHandler = scrollHandler;
}
@Override
public void run() {
// ..
_scrollHandler.sendEmptyMessage(0);
}
}
//活动中的某个地方:
_promoScroller = new PromoScroller(new Handler() {
@Override
public void dispatchMessage(Message msg) {
super.dispatchMessage(msg);
// !! catch message and start the activity
Intent = new Intent(YourCurrentActivty.this, YourTargetActivity.class);
}
});
_scrollerThread = new Thread(_promoScroller);
_scrollerThread.start();
P.S。这些是我用于滚动计时器的代码,但你明白了
<强> UPD 强>
// TASK
public class YourTimerTask extends TimerTask {
private Handler _Handler;
public YourTimerTask(Handler handler) {
_Handler = handler;
}
public void run() {
_Handler.sendEmptyMessage(0);
}
}
// TASK HANDLER (private property in your acitivity)
private Handler _taskHandler = new Handler(){
public void dispatchMessage(android.os.Message msg) {
// do cleanup, close db cursors, file handler, etc.
// start your target activity
Intent viewTargetActivity = new Intent(YourCurrentActivity.this, YourTargetActivity.class);
};
};
// IN YOUR ACTIVITY (for isntance, in onResume method)
Timer timer = new Timer();
timer.schedule(new YourTimerTask(_taskHandler), seconds*1000);
这应该可以胜任。对于计时器 - 只需google。timer example
<强> UPD2 强>
我的错误 - 它应该是Handler _timerHandler = ...
。开始活动时看here
答案 1 :(得分:0)
直截了当的方法:
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import java.util.Timer;
import java.util.TimerTask;
public class SplashScreen extends AppCompatActivity {
WebView wvMain;//This WebView will display the web address provided
Handler handler;//This will be used in the TimerTask
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
handler = new Handler();
wvMain = findViewById(R.id.wvMain);
wvMain.setWebViewClient(new WebViewClient());
wvMain.loadUrl("http://www.sedsworld.com/privacystatement");
wvMain.getSettings().setJavaScriptEnabled(true);
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
//Load second View
Intent intent = new Intent(SplashScreen.this, MainActivity.class);
startActivity(intent);
}
});
}
};
Timer timer = new Timer();
timer.schedule(timerTask, 2000);//Wait 2 seconds then run the timer
}
}