I am trying to change the URL in webView from within a timer class, and as soon as the timer fires, the application crashes:
public class MainActivity extends Activity {
public static WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
Timer xxmyTimer = new Timer();
xxmyTimer.schedule(new TimerTask() {
public void run() {
mWebView.loadUrl("http://somesite.com/");
}
},5000,5000);
}
I've been trying to solve this all day, and no matter what I try, the result is always the same - crash & burn !
Would really appreciate some help with this.
Application is crashing because you are trying access or update UI element(WebView) from the TimerTask which run on separate non UI Thread.
So in current code just add runOnUiThread for accessing WeView from TimerTask Thread as:
xxmyTimer.schedule(new TimerTask() {
public void run() {
MainActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
mWebView.loadUrl("http://somesite.com/");
}
});
}
},5000,5000);
and other option is use Handler.postDelayed instead of TimerTask.
Related
I just wanted to test Log.i() and look at the console in android studio. In the code below onResume should start the thread and run() should write an endless stream of "dings" with the tag "run" in the monitor. But the run method apparently only gets called once. Why?
public class MainActivity extends Activity implements Runnable {
Thread gameThread = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("onCreate","getting started");
}
public void run() {
Log.i("run","ding");
}
#Override
public void onResume() {
super.onResume();
gameThread = new Thread(this);
gameThread.start();
}
}
You're missing the notion of what threading really does. It allows you to run a unit of work asynchronously. So, all the same normal rules apply. The reason it only runs once, is because the thread exits after run() returns. So just like any other method, you should put something like
while(true)
{
Log.i("run","ding");
}
inside of run(). Ideally you would actually check some condition so that you can exit the thread as needed.
Finally, it is probably a bad idea to have your MainActivity implement Runnable. Typically it is good style to have a thread implemented by its own class, for example DingThread implements Runnable.
You're missing while loop that why its run only once. Use below code. This is the better approach to use thread concept.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("onCreate","getting started");
}
#Override
public void onResume() {
super.onResume();
startThread();// create thread obj and start
}
private GameThread mGameThread = null;
private void startThread() {
stopThread();// if thread already running stop it then create new thread and start (for avoiding multi-threading).
mGameThread = new GameThread();
mGameThread.start();//start the thread.
}
//To stop the thread simply call this method.
private void stopThread() {
if(mGameThread != null) {
mGameThread.setStop();
mGameThread = null;
}
}
private class GameThread extends Thread {
private boolean mIsStop;// mIsStop is default false
#Override
public void run() {
while (!mIsStop) { // if mIsStop is false then only come inside loop.
Log.i("run","ding"); //log will print
}
}
public void setStop() {
mIsStop = true;// set mIStop variable to true.
}
}
}
I want to execute a piece of code
So that users can pay attention to my LOGO
Like other APP
I want to use timer Execution setContentView.
Compiler is not wrong.
But execution error.
unfortunately has stopped.
Timer timer;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
abc();
}
public void abc() {
timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
goToLayout1();
}
private void goToLayout1() {
setContentView(R.layout.activity_main2);
timer.cancel();
}
}, 1000, 2000);
}
}
I suggest you should have two activity.First activity is for showing splash screen.You can finish this after 3 seconds and start another activity that is activity with the layout actitiy_main2.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Runnable runnable=new Runnable(){
public void run(){
finish();
//start main activity
}
};
Handler handler=new Handler();
handler.postDelayed(runnable,3000);
}
you are attempting to modify the UI thread from a non-UI thread (a simple java timer)
so please, take care of it and read this Update UI from Thread. Then please, post the exception!
Here's my code:
public class SomeName extends MapActivity implements OnClickListener, OnTouchListener{
public Timer t1 = new Timer();
public TimerTask tt;
public long interval = 5000;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.map);
timer();
}
public final void timer()
{
t1 = new Timer();
tt = new TimerTask() {
#Override
public void run() {
systemClick();
}
};
t1.scheduleAtFixedRate(tt, 10000, interval);
}
public void systemClick()
{
Toast.makeText(getApplicationContext(),"System Button Clicked", 5).show();
}
Actually, I want to call some function, where I refresh my location.
But I can't understand why I never get the toast on the screen.
I'm new to android.
Thanks for any help.
use handler in your Activity
final Handler handlerforadd = new Handler();
Runnable runnableforadd = new Runnable() {
#Override
public void run() {
handlerforadd.postDelayed(this, 1000);
}
};
handlerforadd.postDelayed(runnableforadd, 0);
The reason is the Toast has to be done on the UI thread. In your current code the method run() is being executed on a separate thread. I would suggest looking at this article on Processes and Threads. #parag is correct using a Handler is one way to get a reference to the UI thread but there are other methods.
In the onCreate() method, is there a way to reload URL? I am trying to reload URL every minute
public class AAPActivity extends DroidGap
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
loadUrl("file:///android_asset/www/index.html");
task = new TimerTask(){
public void run() {
handler.post(new Runnable() {
public void run() {
// Here instead of loadURL, is there a way to reload?
loadUrl("file:///android_asset/www/index.html");
}
});
}};
t.schedule(task, 0, 10000);
}
}
Thank you
As it is a Phonegap App you will probably use javascript. I found this:
<script type = "text/Javascript">
function refresh() {
window.location.reload(true);
tim = setTimeout("refresh()",60000); // refresh every minute
}
</script>
on http://www.codingforums.com/archive/index.php/t-157966.html . Maybe that helps ;)
I have one activity. OnCreate the activity gets the source (html) of a web page to a string and presents the result (after parsing it a bit) in a textview.
I would like the activity to reload/refresh periodically to always present the latest information.
What is the best solution for this?
First of all... separate the updating logic from your onCreate method. So, for instance, you can create an updateHTML().
Then, you can use a Timer in order to update the page periodically:
public class YourActivity extends Activity {
private Timer autoUpdate;
public void onCreate(Bundle b){
super.onCreate(b);
// whatever you have here
}
#Override
public void onResume() {
super.onResume();
autoUpdate = new Timer();
autoUpdate.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
updateHTML();
}
});
}
}, 0, 40000); // updates each 40 secs
}
private void updateHTML(){
// your logic here
}
#Override
public void onPause() {
autoUpdate.cancel();
super.onPause();
}
}
Notice that I'm canceling the updating task on onPause, and that in this case the updateHTML method is executed each 40 secs (40000 milliseconds). Also, make sure you import these two classes: java.util.Timer and java.util.TimerTask.