The Java audio timer in Android Studio can be utilized to switch activities depending on the drop-down selections by following these steps:
Here's an example:
// Initializing the timer and handler private Handler handler = new Handler(); private int delay; // in milliseconds private Runnable runnable;
// Initializing the spinner and options Spinner spinner = findViewById(R.id.spinner); ArrayAdapter<charsequence> adapter = ArrayAdapter.createFromResource(this, R.array.optionsarray, android.R.layout.simplespinneritem); adapter.setDropDownViewResource(android.R.layout.simplespinnerdropdownitem); spinner.setAdapter(adapter);
// Handling the selection from the spinner spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView parent, View view, int position, long id) { switch (position) { case 0: // Option 1 delay = 5000; // 5 seconds break; case 1: // Option 2 delay = 10000; // 10 seconds break; case 2: // Option 3 delay = 15000; // 15 seconds break; } // Resetting the timer and scheduling the task handler.removeCallbacks(runnable); handler.postDelayed(runnable, delay); }
@Override
public void onNothingSelected(AdapterView<?> parent) {}
});
// Defining the task to be executed runnable = new Runnable() { @Override public void run() { // Checking the selected option and starting the appropriate activity if (spinner.getSelectedItemPosition() == 0) { Intent intent = new Intent(MainActivity.this, Activity1.class); startActivity(intent); } else if (spinner.getSelectedItemPosition() == 1) { Intent intent = new Intent(MainActivity.this, Activity2.class); startActivity(intent); } else if (spinner.getSelectedItemPosition() == 2) { Intent intent = new Intent(MainActivity.this, Activity3.class); startActivity(intent); } } };
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-07-10 02:27:42 +0000
Seen: 12 times
Last updated: Jul 10 '23
What is the usage of JavaScript in IntelliJ IDEA Community Edition?
What is the issue with using java Gson().fromJson to assign BigDecimal values?
How can a custom loop function shape be altered to resemble Swift more accurately?
In what manner can I incorporate Java Enums into Amazon DynamoDB and AWS SDK v2?
How can Django Admin accommodate a variety of formats and locales for its input fields?