ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Android] intent로 다양한 값을 받아보자
    programing/Mobile 2018. 11. 13. 22:52

     

     

    안녕하세요, Einere입니다.

     

    오늘은 android에서 intent를 이용해 다양한 값을 받아오는 방법을 알아보겠습니다.

     

    이번 글을 포스팅하게 된 계기가..

    드래그한 문자열을 공유버튼으로 공유하여 커스텀 액티비티에서 드래그된 문자열을 가져와 화면에 띄우는 과제때문이에요.

     

    이때까지 배운것은 개발자가 직접 putExtra로 key와 value로 넣어주고, get으로 가져오는 방법밖에 몰랐어요.

    그래서 시스템이 자동으로 넣어준 값을 어떻게 가져오는지 찾는데.. 

    dragged string으로 검색하니 죄다 drag and drop관련 글밖에 안나와서 빡쳤습니다..

     

    그럼 이제 인텐트에서 여러가지 심플 데이터를 받는 방법을 알아보겠습니다.

     

     

     

    activity.java

     

     void onCreate (Bundle savedInstanceState) {     ...     // Get intent, action and MIME type     Intent intent = getIntent();     String action = intent.getAction();     String type = intent.getType();      if (Intent.ACTION_SEND.equals(action) && type != null) {         if ("text/plain".equals(type)) {             handleSendText(intent); // Handle text being sent         } else if (type.startsWith("image/")) {             handleSendImage(intent); // Handle single image being sent         }     } else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {         if (type.startsWith("image/")) {             handleSendMultipleImages(intent); // Handle multiple images being sent         }     } else {         // Handle other intents, such as being started from the home screen     }     ... } 

    기본적인 activity.java의 코드입니다.

     

    intent를 얻은 다음에, text, single image, multiple image를 받는 구조입니다.

    intent를 보낼때, MIME type을 설정하는 이유가, 위와 같이 받는 쪽에서 어떤 데이터형인지 모르기 때문입니다.

     

     

     

    text

     

     void handleSendText(Intent intent) {     String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);     if (sharedText != null) {         // Update UI to reflect text being shared     } }  

    intent에서 text를 받는 경우 입니다.

     

    보통, intent.setExtra("string", string);과 같이 개발자가 임의로 키와 값을 설정하지만,

    시스템이 자동으로 설정한 text data는 위와 같이 받으면 됩니다.

     

     

     

    simple image

     

     void handleSendImage(Intent intent) {     Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);     if (imageUri != null) {         // Update UI to reflect image being shared     } } 

    intent에서 simple image를 받는 경우 입니다.

     

    android에서는 media data를 URI로 표현하고 처리하기 때문에 Uri를 사용합니다.

    아무래도 데이터의 크기가 크다보니 stream을 사용해서 받는것 같습니다.

     

     

     

    multiple image

     

     void handleSendMultipleImages(Intent intent) {     ArrayList imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);     if (imageUris != null) {         // Update UI to reflect multiple images being shared     } } 

    intent에서 multiple image를 받는 경우 입니다.

     

    simple image와 다른점은 ArrayList로 받는다는 것 뿐이네요.

     

     

     

    Caution: Take extra care to check the incoming data, you never know what some other application may send you. For example, the wrong MIME type might be set, or the image being sent might be extremely large. Also, remember to process binary data in a separate thread rather than the main ("UI") thread.

     

    들어오는 데이터의 type을 확인하는 것이 중요하다고 하네요.

    왜냐하면 보내는 쪽에서 잘못 된 MIME type으로 보낼 수 있기 때문입니다.

    또한 매우 큰 데이터를 받는 경우, ANR을 방지하기 위해 main thread말고 work thread에서 작업하는 것이 좋습니다.

     

     

     

    출처

    Receiving simple data from other apps

    댓글

Designed by black7375.