0

嗨,我有一个计时器任务,每 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 的活动。
谢谢。

4

2 回答 2

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();

PS这些是我用于滚动计时器的代码,但你明白了

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);

这应该可以完成这项工作。对于计时器 - 只是谷歌。计时器示例

UPD2

我的错误-应该是Handler _timerHandler = ...。开始活动看这里

于 2012-08-30T12:15:07.990 回答
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
    }
}
于 2018-06-12T19:05:28.290 回答