ㄷㅣㅆㅣ's Amusement

[Android/JAVA] MMS 보내는 코드 본문

Programming/Android

[Android/JAVA] MMS 보내는 코드

ㄷㅣㅆㅣ 2015. 11. 19. 22:26

[Android/JAVA] MMS 보내는 코드

반응형

1. MMS는 SMS와는 달리 그냥 http-post를 하면 된다. 우선 네트워크 피처를 통해서 MMS 전송요청을 한다.

1
2
final ConnectivityManager connMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
final int result = connMgr.startUsingNetworkFeature( ConnectivityManager.TYPE_MOBILE, Phone.FEATURE_ENABLE_MMS);
cs

2. result로 Phone.APN_REQUEST_STARTED를 받으면 BroadCastReciver를 등록하고 Phone.APN_ALREADY_ACTIVE가 올때까지 기다린다.

1
2
3
4
final IntentFilter filter = new IntentFilter();
 
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
context.registerReceiver(reciver, filter);
cs


3. 다음 코드와 같이 OMA스펙에 맞게 pdu를 만들어준다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
final SendReq sendRequest = new SendReq();
    final EncodedStringValue[] sub = EncodedStringValue.extract(subject);
    if (sub != null && sub.length > 0) {
        sendRequest.setSubject(sub[0]);
    }
    final EncodedStringValue[] phoneNumbers = EncodedStringValue
            .extract(recipient);
    if (phoneNumbers != null && phoneNumbers.length > 0) {
        sendRequest.addTo(phoneNumbers[0]);
    }
 
    final PduBody pduBody = new PduBody();
 
    if (parts != null) {
        for (MMSPart part : parts) {
            final PduPart partPdu = new PduPart();
            partPdu.setName(part.Name.getBytes());
            partPdu.setContentType(part.MimeType.getBytes());
            partPdu.setData(part.Data);
            pduBody.addPart(partPdu);
        }
    }
 
    sendRequest.setBody(pduBody);
 
    final PduComposer composer = new PduComposer(this.context, sendRequest);
    final byte[] bytesToSend = composer.make();
 
    HttpUtils.httpConnection(context, 4444L, MMSCenterUrl,
            bytesToSendFromPDU, HttpUtils.HTTP_POST_METHOD, !TextUtils
                    .isEmpty(MMSProxy), MMSProxy, port);
}
cs


4. MMSCenterUrl, MMSProxy, port : MMS-APNs를 조회하여 가져올 수 있다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public class APNHelper {
 
public class APN {
    public String MMSCenterUrl = "";
    public String MMSPort = "";
    public String MMSProxy = ""
}
 
public APNHelper(final Context context) {
    this.context = context;
}   
 
public List<APN> getMMSApns() {     
    final Cursor apnCursor = this.context.getContentResolver().query(Uri.withAppendedPath(Telephony.Carriers.CONTENT_URI, "current"), nullnullnullnull);
if ( apnCursor == null ) {
        return Collections.EMPTY_LIST;
    } else {
        final List<APN> results = new ArrayList<APN>(); 
            if ( apnCursor.moveToFirst() ) {
        do {
            final String type = apnCursor.getString(apnCursor.getColumnIndex(Telephony.Carriers.TYPE));
            if ( !TextUtils.isEmpty(type) && ( type.equalsIgnoreCase(Phone.APN_TYPE_ALL) || type.equalsIgnoreCase(Phone.APN_TYPE_MMS) ) ) {
                final String mmsc = apnCursor.getString(apnCursor.getColumnIndex(Telephony.Carriers.MMSC));
                final String mmsProxy = apnCursor.getString(apnCursor.getColumnIndex(Telephony.Carriers.MMSPROXY));
                final String port = apnCursor.getString(apnCursor.getColumnIndex(Telephony.Carriers.MMSPORT));                  
                final APN apn = new APN();
                apn.MMSCenterUrl = mmsc;
                apn.MMSProxy = mmsProxy;
                apn.MMSPort = port;
                results.add(apn);
            }
        } while ( apnCursor.moveToNext() ); 
             }              
        apnCursor.close();
        return results;
    }
}
 
private Context context;
cs


5. 마무리.

  - MMS를 보내고 나서 보낸 기록을 db에 써야 다른 앱에서도 잘 보일것이다. 이 부분은 나중에 포스팅 하기로 하고... 개인적으로 보내는 것이 받는 것보다 훨신 쉬운 것 같다. 받는 것도 나중에 포스팅;;; 오늘은 이만~


광고는 누르실 필요 없지만 공감버튼은 부탁드려요~~

copy/paste되도록 모든 blocking을 해제했습니다만... 그래도 안되는 분은 OS와 browser를 댓글로 달아주세요~



반응형
Comments