Advertisement
am_dot_com

DDM 2021-12-21

Dec 21st, 2021 (edited)
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 KB | None | 0 0
  1. package com.joythis.android.receiverandasync;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4. import androidx.loader.content.AsyncTaskLoader;
  5.  
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.os.AsyncTask;
  9. import android.os.Bundle;
  10. import android.widget.TextView;
  11.  
  12. public class MainActivity extends AppCompatActivity {
  13.  
  14. Context mContext;
  15. TextView mTvReceivedText;
  16.  
  17. class MyTaskToPost extends AsyncTask<String, Void, String>{
  18.  
  19. @Override
  20. protected String doInBackground(String... strings) {
  21. String url = strings[0];
  22. String when = strings[1];
  23. String what = strings[2];
  24. String strJsonReply = AmIoHttp.postShare(url , when, what);
  25. return strJsonReply;
  26. }//doInBackground
  27.  
  28. @Override
  29. protected void onPostExecute(String s) {
  30. displayTextOnTv(
  31. mTvReceivedText,
  32. s
  33. );
  34. super.onPostExecute(s);
  35. }//onPostExecute
  36. }//MyTaskToPost
  37.  
  38. class MyAsyncTaskForWebTraffic extends AsyncTask<
  39. String, //input data type , a string, e.g. URL
  40. Void, //no progress indicator
  41. String//ouput data type, a string, the source code at URL
  42. >{
  43.  
  44. /*
  45. the code in doInBackground will happen on a separate thread (of the main UI thread)
  46. doInBackground receives an arbitrary number of params
  47. the corresponding args are sent upon calling the "execute" method of the object MyAsyncTaskForWebTraffic
  48. */
  49. @Override
  50. protected String doInBackground(String... strings) {
  51. //return null;
  52. String url = strings[0];
  53. String strHtmlSourceCode = AmIoHttp.io_https_ReadAll(url);
  54. return strHtmlSourceCode;
  55. }
  56.  
  57. /*
  58. when the code in doInBackground ends, onPostExecute will be automatically called
  59. receiving, as an argument, the return of doInBackground
  60. */
  61. @Override
  62. protected void onPostExecute(String s) {
  63. displayTextOnTv(mTvReceivedText, s);
  64. super.onPostExecute(s);
  65. }
  66. }
  67.  
  68. @Override
  69. protected void onCreate(Bundle savedInstanceState) {
  70. super.onCreate(savedInstanceState);
  71. setContentView(R.layout.activity_main);
  72.  
  73. init();
  74. }//onCreate
  75.  
  76. void init(){
  77. mContext = this;
  78. mTvReceivedText = findViewById(R.id.idTvReceivedText);
  79.  
  80. receiveTextFromSharedOtherApps(); //demo1 - receive text from other apps
  81.  
  82. readSourceCodeAtUrl("https://arturmarques.com/"); //demo2 - async reading external contents
  83.  
  84. writeToExternalService(
  85. "https://arturmarques.com/edu/ddm/files/w12/receiver.php",
  86. "2021-12-21 12:17:00",
  87. "okay"
  88. );
  89. }//init
  90.  
  91. void writeToExternalService(
  92. String pUrl,
  93. String pWhen,
  94. String pWhat
  95. ){
  96. //IMPOSSIBLE! Because it would work on the main UI main thread
  97. //AmIoHttp.postShare(pUrl , pWhen, pWhat);
  98.  
  99. //POSSIBLE!
  100. MyTaskToPost t = new MyTaskToPost();
  101. t.execute(pUrl , pWhen, pWhat);
  102. }//writeToExternalService
  103.  
  104. void readSourceCodeAtUrl(String pUrl){
  105. //IMPOSSIBLE => can NOT happen on the main thread
  106. //String strHtmlSourceCode = AmIoHttp.io_https_ReadAll(pUrl);
  107. //displayTextOnTv(mTvReceivedText, strHtmlSourceCode);
  108.  
  109. //POSSIBLE => must happen on a separate thread
  110. //via AsyncTask
  111. MyAsyncTaskForWebTraffic myTask = new MyAsyncTaskForWebTraffic();
  112. myTask.execute("https://arturmarques.com/");
  113. }//readSourceCodeAtUrl
  114.  
  115. void receiveTextFromSharedOtherApps(){
  116. Intent whoCalledMe = this.getIntent();
  117.  
  118. if (whoCalledMe!=null){
  119. String strAction = whoCalledMe.getAction();
  120. boolean bInternalStart = strAction == Intent.ACTION_MAIN;
  121. boolean bExternalStart = strAction == Intent.ACTION_SEND;
  122.  
  123. //an external app is sharing data
  124. if (bExternalStart){
  125. String strType = whoCalledMe.getType();
  126. boolean bIsSomeOtherAppSharingTextWithMe = strType.startsWith("text/");
  127. if (bIsSomeOtherAppSharingTextWithMe){
  128. //a string named "EXTRA_TEXT" transports text between apps
  129. String strTheSharedText =
  130. whoCalledMe.getStringExtra(
  131. Intent.EXTRA_TEXT
  132. );
  133.  
  134. displayTextOnTv (mTvReceivedText, strTheSharedText);
  135. }//if
  136. }//bExternalStart
  137. }//if
  138. }//receiveTextFromSharedOtherApps
  139.  
  140. void displayTextOnTv(
  141. TextView pTv,
  142. String pS
  143. )
  144. {
  145. pTv.setText(pS+"\n"+pTv.getText().toString());
  146. }//displayTextOnTv
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement