에르노트

[안드로이드] 다른 앱에서 공유 목록에 내 앱 띄우기 본문

Dev/Android

[안드로이드] 다른 앱에서 공유 목록에 내 앱 띄우기

두콩 2020. 2. 12. 02:00

안드로이드 앱을 사용하다보면 서로 다른 앱 간 데이터를 공유해야 할 일이 종종 생긴다. 메모 앱의 텍스트 내용을 카톡으로 넘겨주거나 반대로 카카오톡 메세지 내용을 메모장에 공유해서 저장시키는 것이 대표적인 예시라고 볼 수 있다. 그래서 안드로이드에서는 미리 Intent.ACTION_SEND 라는 공유 인텐트를 만들어 두었다. 이를 통해 다른 앱으로 간단한 데이터 보내기를 쉽게 구현할 수 있다.

 

한편 다른 앱이 보낸 간단한 데이터를 받을 앱 목록에 내 앱을 추가하고 싶을 수도 있다. 이때는 manifest에다가 Activity 태그 아래에 intent-filter를 추가해주는 방법으로 역시 손쉽게 처리가 가능하다. 공식 문서는 여기 참조.

 

<intent-filter>
        <action android:name="android.intent.action.SEND"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:mimeType="text/plain"/>
</intent-filter>

여기서 mimeType은 전달받을 데이터 타입이라고 보면 된다. 표로 정리하면 아래와 같다. 

MIME-Type Description File Extension
application/acad AutoCAD drawing files dwg
application/clariscad ClarisCAD files ccad
application/dxf DXF (AutoCAD) dxf
application/msaccess Microsoft Access file mdb
application/msword Microsoft Word file doc
application/octet-stream Uninterpreted binary bin
application/pdf PDF (Adobe Acrobat) pdf
application/postscript Postscript, encapsulated Postscript, ai, ps, eps
Adobe Illustrator
application/rtf Rich Text Format file rtf rtf
application/vnd.ms-excel Microsoft Excel file xls
application/vnd.ms-powerpoint Microsoft PowerPoint file ppt
application/x-cdf Channel Definition Format file cdf
application/x-csh C-shell script csh csh
application/x-dvi TeX dvi dvi dvi
application/x-javascript Javascript source file js
application/x-latex LaTeX source file latex
application/x-mif FrameMaker MIF format mif
application/x-msexcel Microsoft Excel file xls
application/x-mspowerpoint Microsoft PowerPoint file ppt
application/x-tcl TCL script tcl
application/x-tex TeX source file tex
application/x-texinfo Texinfo (emacs) texinfo, texi
application/x-troff troff file t, tr, roff t, tr, roff
application/x-troff-man troff with MAN macros man
application/x-troff-me troff with ME macros me
application/x-troff-ms troff with MS macros ms
application/x-wais-source WAIS source file src
application/zip ZIP archive zip
audio/basic Basic audio (usually m-law) au, snd
audio/x-aiff AIFF audio aif, aiff, aifc
audio/x-wav Windows WAVE audio wav
image/gif GIF image gif
image/ief Image Exchange Format file ief
image/jpeg JPEG image jpeg, jpg jpe
image/tiff TIFF image tiff, tif
image/x-cmu-raster CMU Raster image ras
image/x-portable-anymap PBM Anymap image format pnm
image/x-portable-bitmap PBM Bitmap image format pbm
image/x-portable-graymap PBM Graymap image format pgm
image/x-portable-pixmap PBM Pixmap image format ppm
image/x-rgb RGB image format rgb
image/x-xbitmap X Bitmap image xbm
image/x-xpixmap X Pixmap image xpm
image/x-xwindowdump X Windows Dump (xwd) xwd
multipart/x-gzip GNU ZIP archive gzip
multipart/x-zip PKZIP archive zip
text/css Cascading style sheet css
text/html HTML file html, htm
text/plain Plain text txt
text/richtext MIME Rich Text rtx
text/tab-separated- values Text with tab-separated values tsv
text/xml XML document xml
text/x-setext Struct-Enhanced text etx
text/xsl XSL style sheet xsl
video/mpeg MPEG video mpeg, mpg, mpe
video/quicktime QuickTime video qt, mov
video/x-msvideo Microsoft Windows video avi
video/x-sgi-movie SGI movie player format movie

 

공유 받은 데이터를 사용하는 방법 역시 간단한다. 명시적 인텐트를 처리하는 느낌 그대로 intent.action과 intent.type을 체크한 후 데이터를 사용하면 된다. 일반적인 텍스트를 전달받는 상황이라면 다음과 같이 전달받은 텍스트를 받아와서 토스트로 띄워줄 수 있을 것이다.

if(intent.action == Intent.ACTION_SEND && intent.type == "text/plain")
            toast("전달 받은 텍스트: ${intent.getStringExtra(Intent.EXTRA_TEXT)}")

 

Comments