Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TestActivity extends Activity {
- Button btn;
- int i = 0;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- btn = (Button)findViewById(R.id.btn);
- btn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- runThread();
- }
- });
- }
- private void runThread(){
- runOnUiThread (new Thread(new Runnable() {
- public void run() {
- while(i++ < 1000){
- btn.setText("#"+i);
- try {
- Thread.sleep(300);
- }
- catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }));
- }
- }
- private void runThread() {
- new Thread() {
- public void run() {
- while (i++ < 1000) {
- try {
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- btn.setText("#" + i);
- }
- });
- Thread.sleep(300);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }.start();
- }
- public void debugMsg(String msg) {
- final String str = msg;
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- mInfo.setText(str);
- }
- });
- }
- runOnUiThread(new Runnable() {
- public void run() {
- //Do something on UiThread
- }
- });
- public class AndroidBasicThreadActivity extends AppCompatActivity
- {
- public static TextView textView;
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_android_basic_thread);
- textView = (TextView) findViewById(R.id.textview);
- MyAndroidThread myTask = new MyAndroidThread(AndroidBasicThreadActivity.this);
- Thread t1 = new Thread(myTask, "Bajrang");
- t1.start();
- }
- }
- class MyAndroidThread implements Runnable
- {
- Activity activity;
- public MyAndroidThread(Activity activity)
- {
- this.activity = activity;
- }
- @Override
- public void run()
- {
- activity.runOnUiThread(new Runnable()
- {
- @Override
- public void run()
- {
- AndroidBasicThreadActivity.textView.setText("Hello!! Android Team :-) From child thread.");
- }
- });
- }
- }
- class MyAndroidThread implements Runnable
- {
- Activity activity;
- public MyAndroidThread(Activity activity)
- {
- this.activity = activity;
- }
- @Override
- public void run()
- {
- AndroidBasicThreadActivity.textView.post(new Runnable()
- {
- @Override
- public void run()
- {
- AndroidBasicThreadActivity.textView.setText("Hello!! Android Team :-) From child thread.");
- }
- });
- }
- }
- class MyAndroidThread implements Runnable
- {
- Activity activity;
- public MyAndroidThread(Activity activity)
- {
- this.activity = activity;
- }
- @Override
- public void run()
- {
- new Handler(Looper.getMainLooper()).post(new Runnable() {
- public void run() {
- AndroidBasicThreadActivity.textView.setText("Hello!! Android Team :-) From child thread.");
- }
- });
- }
- }
- public class MainActivity extends AppCompatActivity {
- class SetSynonymResult implements Runnable {
- String synonym;
- SetSynonymResult(String synonym) {
- this.synonym = synonym;
- }
- public void run() {
- Log.d("AsyncAndroid", String.format("Sending synonym result %s on %d",
- synonym, Thread.currentThread().getId()) + " !");
- TextView tv = (TextView) findViewById(R.id.synonymTv);
- tv.setText(this.synonym);
- }
- }
- ;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Button search = (Button) findViewById(R.id.searchBut);
- final EditText word = (EditText) findViewById(R.id.wordEt);
- search.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Runnable searchTask = new Runnable() {
- @Override
- public void run() {
- String result = searchSynomim(word.getText().toString());
- Log.d("AsyncAndroid", String.format("Searching for synonym for %s on %s",
- word.getText(), Thread.currentThread().getName()));
- runOnUiThread(new SetSynonymResult(result));
- }
- };
- Thread thread = new Thread(searchTask);
- thread.start();
- }
- });
- }
- static int i = 0;
- String searchSynomim(String word) {
- return ++i % 2 == 0 ? "fake" : "mock";
- }
- }
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- //Do something on UiThread
- }
- });
- @UiThread
- public void logMsg(final String msg) {
- new Handler(Looper.getMainLooper()).post(new Runnable() {
- @Override
- public void run() {
- Log.d("UI thread", "I am the UI thread");
- }
- });
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- gifImageView = (GifImageView) findViewById(R.id.GifImageView);
- gifImageView.setGifImageResource(R.drawable.success1);
- new Thread(new Runnable() {
- @Override
- public void run() {
- try {
- //dummy delay for 2 second
- Thread.sleep(8000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- //update ui on UI thread
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- gifImageView.setGifImageResource(R.drawable.success);
- }
- });
- }
- }).start();
- }
Add Comment
Please, Sign In to add comment