0

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.

El Sa7eR
  • 881
  • 3
  • 10
  • 17
  • I think you are looking for something like: [link]http://stackoverflow.com/questions/4298225/how-can-i-start-an-activity-from-a-non-activity-class – Mr.S Aug 30 '12 at 12:13

2 Answers2

1

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

P.S. 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

Community
  • 1
  • 1
midnight
  • 3,420
  • 3
  • 36
  • 58
  • Thanks , but how I can relate the handler with the timer task ? – El Sa7eR Aug 30 '12 at 12:36
  • you can pass an instance of handler when creating your task. My threads constructor specifies Handler to be present so you wright something similar to _scrollerThread = new Thread(YOURHANDLER); Inside your task class you should have a property for your handler: my is ` private Handler _scrollHandler;` – midnight Aug 30 '12 at 12:44
  • Thanks , but can you help me with the code , sorry for bad knowledge , but I am new with Java and android development , I have switched from C# .net , all I have done is create this class public class RunListener implements Runnable { private Handler _Handler; public RunListener(Handler handler) { _Handler = handler; } public void run() { _Handler.sendEmptyMessage(0); } } please help me with next step – El Sa7eR Aug 30 '12 at 13:19
  • Thanks a lot , but MyTimerTask is related to a service not an activity , this service , run an activity. – El Sa7eR Aug 30 '12 at 13:52
  • Thanks , but this doesn't work for me :( , is there onResume method for the service ? MyTimerTask is related to service not activity. – El Sa7eR Aug 30 '12 at 14:54
0

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
    }
}
SedJ601
  • 12,173
  • 3
  • 41
  • 59