Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.joythis.android.receiverandasync;
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.loader.content.AsyncTaskLoader;
- import android.content.Context;
- import android.content.Intent;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.widget.TextView;
- public class MainActivity extends AppCompatActivity {
- Context mContext;
- TextView mTvReceivedText;
- class MyTaskToPost extends AsyncTask<String, Void, String>{
- @Override
- protected String doInBackground(String... strings) {
- String url = strings[0];
- String when = strings[1];
- String what = strings[2];
- String strJsonReply = AmIoHttp.postShare(url , when, what);
- return strJsonReply;
- }//doInBackground
- @Override
- protected void onPostExecute(String s) {
- displayTextOnTv(
- mTvReceivedText,
- s
- );
- super.onPostExecute(s);
- }//onPostExecute
- }//MyTaskToPost
- class MyAsyncTaskForWebTraffic extends AsyncTask<
- String, //input data type , a string, e.g. URL
- Void, //no progress indicator
- String//ouput data type, a string, the source code at URL
- >{
- /*
- the code in doInBackground will happen on a separate thread (of the main UI thread)
- doInBackground receives an arbitrary number of params
- the corresponding args are sent upon calling the "execute" method of the object MyAsyncTaskForWebTraffic
- */
- @Override
- protected String doInBackground(String... strings) {
- //return null;
- String url = strings[0];
- String strHtmlSourceCode = AmIoHttp.io_https_ReadAll(url);
- return strHtmlSourceCode;
- }
- /*
- when the code in doInBackground ends, onPostExecute will be automatically called
- receiving, as an argument, the return of doInBackground
- */
- @Override
- protected void onPostExecute(String s) {
- displayTextOnTv(mTvReceivedText, s);
- super.onPostExecute(s);
- }
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- init();
- }//onCreate
- void init(){
- mContext = this;
- mTvReceivedText = findViewById(R.id.idTvReceivedText);
- receiveTextFromSharedOtherApps(); //demo1 - receive text from other apps
- readSourceCodeAtUrl("https://arturmarques.com/"); //demo2 - async reading external contents
- writeToExternalService(
- "https://arturmarques.com/edu/ddm/files/w12/receiver.php",
- "2021-12-21 12:17:00",
- "okay"
- );
- }//init
- void writeToExternalService(
- String pUrl,
- String pWhen,
- String pWhat
- ){
- //IMPOSSIBLE! Because it would work on the main UI main thread
- //AmIoHttp.postShare(pUrl , pWhen, pWhat);
- //POSSIBLE!
- MyTaskToPost t = new MyTaskToPost();
- t.execute(pUrl , pWhen, pWhat);
- }//writeToExternalService
- void readSourceCodeAtUrl(String pUrl){
- //IMPOSSIBLE => can NOT happen on the main thread
- //String strHtmlSourceCode = AmIoHttp.io_https_ReadAll(pUrl);
- //displayTextOnTv(mTvReceivedText, strHtmlSourceCode);
- //POSSIBLE => must happen on a separate thread
- //via AsyncTask
- MyAsyncTaskForWebTraffic myTask = new MyAsyncTaskForWebTraffic();
- myTask.execute("https://arturmarques.com/");
- }//readSourceCodeAtUrl
- void receiveTextFromSharedOtherApps(){
- Intent whoCalledMe = this.getIntent();
- if (whoCalledMe!=null){
- String strAction = whoCalledMe.getAction();
- boolean bInternalStart = strAction == Intent.ACTION_MAIN;
- boolean bExternalStart = strAction == Intent.ACTION_SEND;
- //an external app is sharing data
- if (bExternalStart){
- String strType = whoCalledMe.getType();
- boolean bIsSomeOtherAppSharingTextWithMe = strType.startsWith("text/");
- if (bIsSomeOtherAppSharingTextWithMe){
- //a string named "EXTRA_TEXT" transports text between apps
- String strTheSharedText =
- whoCalledMe.getStringExtra(
- Intent.EXTRA_TEXT
- );
- displayTextOnTv (mTvReceivedText, strTheSharedText);
- }//if
- }//bExternalStart
- }//if
- }//receiveTextFromSharedOtherApps
- void displayTextOnTv(
- TextView pTv,
- String pS
- )
- {
- pTv.setText(pS+"\n"+pTv.getText().toString());
- }//displayTextOnTv
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement