Guest User

Untitled

a guest
Feb 20th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.58 KB | None | 0 0
  1. public class TestActivity extends Activity {
  2.  
  3. Button btn;
  4. int i = 0;
  5.  
  6. @Override
  7. public void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.main);
  10. btn = (Button)findViewById(R.id.btn);
  11. btn.setOnClickListener(new View.OnClickListener() {
  12. @Override
  13. public void onClick(View v) {
  14. runThread();
  15. }
  16. });
  17. }
  18.  
  19. private void runThread(){
  20. runOnUiThread (new Thread(new Runnable() {
  21. public void run() {
  22. while(i++ < 1000){
  23. btn.setText("#"+i);
  24. try {
  25. Thread.sleep(300);
  26. }
  27. catch (InterruptedException e) {
  28. e.printStackTrace();
  29. }
  30. }
  31. }
  32. }));
  33. }
  34. }
  35.  
  36. private void runThread() {
  37.  
  38. new Thread() {
  39. public void run() {
  40. while (i++ < 1000) {
  41. try {
  42. runOnUiThread(new Runnable() {
  43.  
  44. @Override
  45. public void run() {
  46. btn.setText("#" + i);
  47. }
  48. });
  49. Thread.sleep(300);
  50. } catch (InterruptedException e) {
  51. e.printStackTrace();
  52. }
  53. }
  54. }
  55. }.start();
  56. }
  57.  
  58. public void debugMsg(String msg) {
  59. final String str = msg;
  60. runOnUiThread(new Runnable() {
  61. @Override
  62. public void run() {
  63. mInfo.setText(str);
  64. }
  65. });
  66. }
  67.  
  68. runOnUiThread(new Runnable() {
  69. public void run() {
  70. //Do something on UiThread
  71. }
  72. });
  73.  
  74. public class AndroidBasicThreadActivity extends AppCompatActivity
  75. {
  76. public static TextView textView;
  77. @Override
  78. protected void onCreate(Bundle savedInstanceState)
  79. {
  80. super.onCreate(savedInstanceState);
  81. setContentView(R.layout.activity_android_basic_thread);
  82.  
  83. textView = (TextView) findViewById(R.id.textview);
  84.  
  85. MyAndroidThread myTask = new MyAndroidThread(AndroidBasicThreadActivity.this);
  86. Thread t1 = new Thread(myTask, "Bajrang");
  87. t1.start();
  88. }
  89. }
  90.  
  91. class MyAndroidThread implements Runnable
  92. {
  93. Activity activity;
  94. public MyAndroidThread(Activity activity)
  95. {
  96. this.activity = activity;
  97. }
  98. @Override
  99. public void run()
  100. {
  101. activity.runOnUiThread(new Runnable()
  102. {
  103. @Override
  104. public void run()
  105. {
  106. AndroidBasicThreadActivity.textView.setText("Hello!! Android Team :-) From child thread.");
  107. }
  108. });
  109. }
  110. }
  111.  
  112. class MyAndroidThread implements Runnable
  113. {
  114. Activity activity;
  115. public MyAndroidThread(Activity activity)
  116. {
  117. this.activity = activity;
  118. }
  119. @Override
  120. public void run()
  121. {
  122. AndroidBasicThreadActivity.textView.post(new Runnable()
  123. {
  124. @Override
  125. public void run()
  126. {
  127. AndroidBasicThreadActivity.textView.setText("Hello!! Android Team :-) From child thread.");
  128. }
  129. });
  130.  
  131. }
  132. }
  133.  
  134. class MyAndroidThread implements Runnable
  135. {
  136. Activity activity;
  137. public MyAndroidThread(Activity activity)
  138. {
  139. this.activity = activity;
  140. }
  141. @Override
  142. public void run()
  143. {
  144. new Handler(Looper.getMainLooper()).post(new Runnable() {
  145. public void run() {
  146. AndroidBasicThreadActivity.textView.setText("Hello!! Android Team :-) From child thread.");
  147. }
  148. });
  149. }
  150. }
  151.  
  152. public class MainActivity extends AppCompatActivity {
  153.  
  154. class SetSynonymResult implements Runnable {
  155. String synonym;
  156.  
  157. SetSynonymResult(String synonym) {
  158. this.synonym = synonym;
  159. }
  160.  
  161. public void run() {
  162. Log.d("AsyncAndroid", String.format("Sending synonym result %s on %d",
  163. synonym, Thread.currentThread().getId()) + " !");
  164. TextView tv = (TextView) findViewById(R.id.synonymTv);
  165. tv.setText(this.synonym);
  166. }
  167. }
  168.  
  169. ;
  170.  
  171. @Override
  172. protected void onCreate(Bundle savedInstanceState) {
  173. super.onCreate(savedInstanceState);
  174. setContentView(R.layout.activity_main);
  175.  
  176. Button search = (Button) findViewById(R.id.searchBut);
  177. final EditText word = (EditText) findViewById(R.id.wordEt);
  178. search.setOnClickListener(new View.OnClickListener() {
  179. @Override
  180. public void onClick(View v) {
  181. Runnable searchTask = new Runnable() {
  182. @Override
  183. public void run() {
  184. String result = searchSynomim(word.getText().toString());
  185. Log.d("AsyncAndroid", String.format("Searching for synonym for %s on %s",
  186. word.getText(), Thread.currentThread().getName()));
  187. runOnUiThread(new SetSynonymResult(result));
  188. }
  189. };
  190. Thread thread = new Thread(searchTask);
  191. thread.start();
  192. }
  193. });
  194.  
  195. }
  196.  
  197. static int i = 0;
  198.  
  199. String searchSynomim(String word) {
  200. return ++i % 2 == 0 ? "fake" : "mock";
  201. }
  202. }
  203.  
  204. runOnUiThread(new Runnable() {
  205. @Override
  206. public void run() {
  207. //Do something on UiThread
  208. }
  209. });
  210.  
  211. @UiThread
  212. public void logMsg(final String msg) {
  213. new Handler(Looper.getMainLooper()).post(new Runnable() {
  214. @Override
  215. public void run() {
  216. Log.d("UI thread", "I am the UI thread");
  217.  
  218.  
  219. }
  220. });
  221. }
  222.  
  223. @Override
  224. protected void onCreate(Bundle savedInstanceState) {
  225. super.onCreate(savedInstanceState);
  226. setContentView(R.layout.activity_main);
  227.  
  228. gifImageView = (GifImageView) findViewById(R.id.GifImageView);
  229. gifImageView.setGifImageResource(R.drawable.success1);
  230.  
  231. new Thread(new Runnable() {
  232. @Override
  233. public void run() {
  234. try {
  235. //dummy delay for 2 second
  236. Thread.sleep(8000);
  237. } catch (InterruptedException e) {
  238. e.printStackTrace();
  239. }
  240.  
  241. //update ui on UI thread
  242. runOnUiThread(new Runnable() {
  243. @Override
  244. public void run() {
  245. gifImageView.setGifImageResource(R.drawable.success);
  246. }
  247. });
  248.  
  249. }
  250. }).start();
  251.  
  252. }
Add Comment
Please, Sign In to add comment