Timer and setContentView on Android - android

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!

Related

Simplest way to call a method in Thread using Handler after every 10 seconds

I have created a method and i want to call it after every 10 seconds with the help of Thread in Handler. My code is..
public void saveDataToServer(){
//do logic here
}
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Handler().postDelayed(
new Runnable() {
#Override
public void run() {
saveDataToServer();
}
},
10000);
}
There is no error in code, But unfortunately not running. Can anybody please tell what i am doing wrong.. Thanks in advance.
You should call
new Handler().postDelayed(this,10000);
in Run like
Runnable r=new Runnable() {
#Override
public void run() {
saveDataToServer();
new Handler().postDelayed(this,10000);
}
};
new Handler().postDelayed(r,10000);

Updating WebView URL from a Timer Class in Android

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.

How to pause handler.postDelayed() timer on Android

How can i pause the handler.postDelayed() timer using a button. So when i click the same button again the handler.postDelayed() timer should resume.
handler.postDelayed(counterz, 60);
Handler does not have a timer to tweak. You are posting to event-queue of a thread, where a lot of other stuff is running as well.
You can cancel posted Runnable's:
handler.removeCallbacks(counterz);
And post again, to resume.
Handler does not have a pause method. You need to cancel and run again.
http://developer.android.com/reference/android/os/Handler.html#removeCallbacks(java.lang.Runnable)
public final void removeCallbacks (Runnable r)
Remove any pending posts of Runnable r that are in the message queue.
When not required you need to call m_handler.removeCallbacks(m_handlerTask) to cancel the run. If you need again you need to run the the task again.
Handler m_handler;
Runnable m_handlerTask ;
m_handler = new Handler();
m_handlerTask = new Runnable()
{
#Override
public void run() {
// do something
m_handler.postDelayed(m_handlerTask, 1000);
}
};
m_handlerTask.run(); // call run
Suppose you use a timer. Even timer does not have pause method.
public class YourActivity extends AppCompatActivity {
private static boolean handlerflag=false;
private Handler handler;
private Runnable runnable;
private int myind=0,index=0,count=0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activtiy);
//oncreate exe only
handlerflag=true;
handler = new Handler();
startyourtime(0);
}
private void startyourtime(int a) {
myind=0;
for (index=a; index<10 ;index++) {
myind++;
runnable=new Runnable() {
count++;
#Override
public void run() {
//your code here
}
};handler.postDelayed(runnable, Constants.TIME_LIMIT * myind);
}
#Override
protected void onPause() {
super.onPause();
handlerflag=false;
handler.removeCallbacksAndMessages(null);
}
#Override
protected void onResume() {
super.onResume();
if(!handlerflag)
{
startyourtime(count);
}
}
}

Delay actions in android

I want to change the image in imageView after 5 seconds from app start.
This is the code that I tried so far:
public class MainActivity extends Activity {
ImageView screen;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
screen=(ImageView)findViewById(R.id.imageView1);
screen.setImageResource(R.drawable.ic_launcher);
}
}
You can use a Handler, such as:
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// change image
}
}, 5000); // 5000ms delay
As Sam says in the comments, you could also do this (because all Views have their own handler):
screen.postDelayed(new Runnable() {
#Override
public void run() {
// change image
}
}, 5000); // 5000ms delay
See the Handler Documentation.
you can try thread like this:
new Thread(){
public void run(){
//sleep(5000);
//refreshSthHere();
}
}.start();
You can use Handler like Tom Leese said or Java Timer
new Timer().schedule(new TimerTask() {
#Override
public void run() {
// this code will be executed after 2 seconds
}
}, 2000);

TimerTask doesn't take effect

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.

Categories

Resources