참고 : 다른앱으로 데이터 전달하기


본 내용은 Android Devloper site 의 내용을 발췌한 내용입니다.


1. 다른 App으로부터 ACTION_SEND 인텐트를 받기 위해서 App의 Manifest 파일에 <intent-filter> 엘리먼트를 추가 한다.
<intent-filter>를 추가 함으로 인해서 앱이 다른 앱으로부터 데이터를 받을 수 있음을 나타낸다. 다른 앱이 쉐어 기능을 활용할 경우 , 동일한 mimeType 으로 지정되어 있으면 공유 리스트에 나타나게 된다.
<activity android:name=".ui.MyActivity" >

   
<intent-filter>

       
<action android:name="android.intent.action.SEND" />

       
<category android:name="android.intent.category.DEFAULT" />

       
<data android:mimeType="image/*" />

   
</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>

   
<intent-filter>

       
<action android:name="android.intent.action.SEND_MULTIPLE" />

       
<category android:name="android.intent.category.DEFAULT" />

       
<data android:mimeType="image/*" />

   
</intent-filter>
</activity>

2. 다른 앱으로부터  전달받은 데이터가 실행될 때는 getIntent() 로  ACTION_SEND Intent로 받은 내용에 따라서 핸들링하는 부분을 다음소스와 같이 추가 해야한다.
홈에서 앱을 띄우는 경우와 다른앱에서 ACTION_SEND 로 전달되는 경우 각각에 따른 핸들링이 필요하다.
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

   
}

   
...
}


void handleSendText(Intent intent) {

   
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);

   
if (sharedText != null) {

       
// Update UI to reflect text being shared

   
}
}


void handleSendImage(Intent intent) {

   
Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);

   
if (imageUri != null) {

       
// Update UI to reflect image being shared

   
}
}


void handleSendMultipleImages(Intent intent) {

   
ArrayList<Uri> imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);

   
if (imageUris != null) {

       
// Update UI to reflect multiple images being shared

   
}
}




+ Recent posts