As a beginner in Android programming, I don't really master the use of the threads and the Handler() function. After clicking on a button, I'd like to call a first method startProjection() then wait a few seconds before call a second method stopProjection ().
By reading some topics about the question, I built this code, able to perform the first call but not the second :
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// some code
// start projection
Button startButton = (Button) findViewById(R.id.startButton);
startButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startProjection();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
stopProjection();
}
}, 7000);
}
});
The Build Gradle works correctly but when I finally click on the Button nothing happens. It seems that the handler doesn't work correctly. Is there anyway to perform these actions in the same onClick() method ?
EDIT : Previously the two methods were called separately buy using two buttons (and it works !), but I really want to use a single button.
I just want to check if you have any specific reason to have stopProjection() function in a seperate thread. May be the following might work fine :
startProjection();
Thread.sleep(7000);
stopProjection();
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.