Reputation:
I was learning the handler thread and I got an issue. It's not working. I gave delay of 3 sec with postDelayed and also used postAtTime but none is working now. And there is no eeror or exception in run tab either.
here is the java code.
package com.example.ims;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.ProgressBar;
import androidx.appcompat.app.AppCompatActivity;
public class SplashScreen extends AppCompatActivity {
ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
init();
int interval = 3000;
new Handler(Looper.getMainLooper()).postAtTime(new Runnable(){
@Override
public void run() {
progressBar.setVisibility(View.VISIBLE);
}
},System.currentTimeMillis()+ interval);
}
void init()
{
progressBar = findViewById(R.id.progressbar);
progressBar.setVisibility(View.GONE);
}
@Override
protected void onStart() {
super.onStart();
}
}
This code should show progressbar after 3 seconds but, it's not showing.
I have gone through these links, But none helped me
Android threading and Handler not working
I visited some other links too. But, Apparantly all are concluding that handler thread should be in onCreate() method, But mine is already in onCreate method.
Upvotes: 0
Views: 688
Reputation: 104
for using postDelayed() you must do this:
int interval = 3000;
new Handler(Looper.getMainLooper()).postDelayed(new Runnable(){
@Override
public void run() {
progressBar.setVisibility(View.VISIBLE);
}
},interval);
but for using postAtTime() you must use SystemClock.uptimeMillis() instead of System.currentTimeMillis(). Because second parameter of postAtTime() get uptime millis and not current time millis.
postAtTime() doc say about it second parameter:
"The absolute time at which the callback should run, using the {@link android.os.SystemClock#uptimeMillis} time-base"
Upvotes: 1