简体   繁体   English

计时器任务的Android开始活动

[英]Android Start activity from timertask

Hi I have a timer task that check a file every 1 minute 嗨,我有一个计时器任务,每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();
}

} }

in the check of file.exists , I want if the file is there , activity called GetGPS. 在file.exists的检查中,我想要文件是否存在,该活动称为GetGPS。
Thanks. 谢谢。

In your case I would recommend using Handler class. 在您的情况下,我建议使用Handler类。 Here's what I would do: 这就是我要做的:

private static class PromoScroller implements Runnable {

    private Handler _scrollHandler;

    public PromoScroller(Handler scrollHandler) {
        _scrollHandler = scrollHandler;
    }

    @Override
    public void run() {
        // .. 
        _scrollHandler.sendEmptyMessage(0);
    }
}

// somewhere in your activity: //您的活动中的某处:

_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 those are bits of code I use for scrolling timer, but you get the idea PS那些是我用于滚动计时器的代码,但是你明白了

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

This should do the job. 这应该做的工作。 For timer - just google. 对于计时器-只是谷歌。 timer example 计时器示例

UPD2 UPD2

my mistake - it should be Handler _timerHandler = ... . 我的错误-应该是Handler _timerHandler = ... for starting activity look here 开始活动请看这里

Straightforward approach: 简单的方法:

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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM