If I want to send an event, for example. OnClick, to activity from a stream? Thank.
Expected workflow below:
public class HelloAndroid extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Crate threadA Start threadA } public void OnSomeEvent() { do something that changes the views in this activity; } private class ThreadA extends Thread { public void run() { do something ... Send Some Event to Activity HelloAndroid. } }
You will need to use Handlers to update the interface.
You can always send a message from a thread to an action, for example:
//this should be in your Activity class private Handler SomeHandler = new Handler() { public void handleMessage(Message msg) { ReactOnMessage(); } }; private class SomeThread implements Runnable { public void run() { doSomething(); SomeHandler.sendEmptyMessage(0); } }
You can also create a message that will contain some values.
UI . http://developer.android.com/guide/appendix/faq/commontasks.html#threading
, OnSomeEvent() HelloAndroid ThreadA, ?
OnSomeEvent()
HelloAndroid
ThreadA
If so, you can fix:
private class ThreadA extends Thread { public void run() { HelloAndroid.this.OnSomeEvent(); } }
or even simpler, just call the method OnSomeEvent()directly.
Source: https://habr.com/ru/post/1730304/More articles:WinForms performance secret: is the result of PictureBox control slower than the original? - performance13 '' iPad simulator - ipadBinary and non-binary socketing java - javaCan you make a C ++ application call from MS SQL Server? - c ++Unit test preparation: what is important to keep in mind when working on software architecture? - unit-testingDownload additional comments using AJAX in Rails - ajaxAre .fsx files interactively compiled in debug or release mode? - f #C ++ Allow host IP from URL - c ++Display keyboard shortcuts in context menus Visual Studio - visual-studioHow to implement a function that can take arbitrary parameters in PHP? - functionAll Articles