How to make a toast every 10 seconds using a ScheduledExecutorService?

log "hello" appears only once, and the toast does not appear at all.

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); scheduler.scheduleAtFixedRate(new Runnable() { public void run() { Log.i("hello", "world"); Toast.makeText(getApplicationContext(), "It works", Toast.LENGTH_SHORT).show(); // TODO Auto-generated method stub } }, 10, 10, TimeUnit.SECONDS); } 
+4
source share
1 answer

Try

  scheduler.scheduleAtFixedRate(new Runnable() { public void run() { Log.i("hello", "world"); runOnUiThread(new Runnable() { public void run() { Toast.makeText(getApplicationContext(), "It works", Toast.LENGTH_SHORT).show(); } }); } }, 10, 10, TimeUnit.SECONDS); 
+8
source

Source: https://habr.com/ru/post/1398352/


All Articles