简体   繁体   中英

Android Start activity from timertask

Hi I have a timer task that check a file every 1 minute

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.
Thanks.

In your case I would recommend using Handler class. 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

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

my mistake - it should be 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
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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