tufanakcay / RunnableHandler

Runnable Handler

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Runnable Handler

In Android development, a Runnable is an interface that represents a unit of work that can be executed asynchronously on a thread. It's often used in conjunction with a Handler to schedule and manage tasks on the main/UI thread or other worker threads. Here's a basic overview of how you can use Runnable and Handler in an Android application:

public class MyActivity extends AppCompatActivity {

    private Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Runnable myRunnable = new Runnable() {
            @Override
            public void run() {
                // Code to be executed asynchronously
                // For example, update UI elements here
            }
        };

        handler.postDelayed(myRunnable, 2000);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
    }
}

About

Runnable Handler

License:GNU General Public License v3.0


Languages

Language:Java 100.0%