am_dot_com

PDM2 20210105

Jan 5th, 2021 (edited)
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
XML 3.72 KB | None | 0 0
  1. <intent-filter>
  2. <action android:name="android.intent.action.SEND" />
  3. <category android:name="android.intent.category.DEFAULT" />
  4. <data android:mimeType="text/*"/>
  5. </intent-filter>
  6.  
  7. <intent-filter>
  8. <action android:name="android.intent.action.SEND" />
  9. <category android:name="android.intent.category.DEFAULT" />
  10. <data android:mimeType="image/*"/>
  11. </intent-filter>
  12.  
  13. //
  14.  
  15. <?xml version="1.0" encoding="utf-8"?>
  16. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  17.    xmlns:app="http://schemas.android.com/apk/res-auto"
  18.    xmlns:tools="http://schemas.android.com/tools"
  19.    android:layout_width="match_parent"
  20.    android:layout_height="match_parent"
  21.    tools:context=".MainActivity">
  22.  
  23.     <TextView
  24.        android:gravity="center_horizontal"
  25.        app:layout_constraintTop_toTopOf="parent"
  26.        android:id="@+id/idTvAbout"
  27.        android:layout_width="match_parent"
  28.        android:layout_height="wrap_content"
  29.        android:text="@string/strTvAbout"/>
  30.  
  31.     <TextView
  32.        app:layout_constraintTop_toBottomOf="@id/idTvAbout"
  33.        android:id="@+id/idTvCapturedText"
  34.        android:layout_width="match_parent"
  35.        android:layout_height="wrap_content"
  36.    />
  37.  
  38. </androidx.constraintlayout.widget.ConstraintLayout>
  39.  
  40. //
  41. package com.joythis.android.pdm2textcapturer;
  42.  
  43. import androidx.appcompat.app.AppCompatActivity;
  44.  
  45. import android.content.Context;
  46. import android.content.Intent;
  47. import android.os.Bundle;
  48. import android.widget.TextView;
  49.  
  50. public class MainActivity extends AppCompatActivity {
  51.     Context mContext;
  52.  
  53.     TextView mTvAbout, mTvCapturedText;
  54.  
  55.     @Override
  56.     protected void onCreate(Bundle savedInstanceState) {
  57.         super.onCreate(savedInstanceState);
  58.         setContentView(R.layout.activity_main);
  59.  
  60.         init();
  61.     }//onCreate
  62.  
  63.     void init(){
  64.         mContext = this;
  65.  
  66.         mTvAbout = findViewById(R.id.idTvAbout);
  67.         mTvCapturedText = findViewById (R.id.idTvCapturedText);
  68.  
  69.         checkIfCalledByAnotherAppAndReceiveItsSharedData();
  70.     }//init
  71.  
  72.     void checkIfCalledByAnotherAppAndReceiveItsSharedData(){
  73.         Intent intentHowWasICalled = getIntent();
  74.  
  75.         if (intentHowWasICalled!=null){
  76.             String strAction = intentHowWasICalled.getAction();
  77.  
  78.             boolean bIsItActionSend =
  79.                 strAction.equals(Intent.ACTION_SEND);
  80.  
  81.             //"banana".compareTo("Banana")!=0
  82.             //"banana".compareToIgnoreCase("Banana")==0
  83.             boolean bIsItActionSend2 =
  84.                 strAction.compareTo(Intent.ACTION_SEND)==0;
  85.  
  86.             boolean bIsItActionSend3 =
  87.                 strAction.compareToIgnoreCase(Intent.ACTION_SEND)==0;
  88.  
  89.             //is this a share situation?
  90.             if (bIsItActionSend){
  91.                 String strType =
  92.                     intentHowWasICalled.getType();
  93.  
  94.                 boolean bIsItSharedText =
  95.                     strType.startsWith("text/");
  96.  
  97.                 boolean bIsItSharedImage =
  98.                     strType.startsWith("image/");
  99.  
  100.                 if (bIsItSharedText){
  101.                     //TODO: we can handle text!
  102.                     //receive the shared text
  103.                     String strSharedText =
  104.                         intentHowWasICalled.getStringExtra(
  105.                             Intent.EXTRA_TEXT
  106.                         );
  107.  
  108.                     //display the received text in mTvCapturedText
  109.                     mTvCapturedText.setText(strSharedText);
  110.                 }//if
  111.  
  112.                 if (bIsItSharedImage){
  113.                     //TODO: how to handle image sharing?
  114.                 }
  115.             }//if
  116.         }//if
  117.     }//checkIfCalledByAnotherAppAndReceiveItsSharedData
  118. }//MainActivity
Add Comment
Please, Sign In to add comment