Как и в SmsManager, есть что-то вроде API-интерфейса MmsManager, которое не доступно для платформы Android.
Итак, для отправки MMS сначала вам нужно приобрести сетевую MMS-связь, а затем совершать http-вызовы.
/**
* Synchronously acquire MMS network connectivity
*
* @throws MmsNetworkException If failed permanently or timed out
*/
void acquireNetwork() throws MmsNetworkException {
Log.i(MmsService.TAG, "Acquire MMS network");
synchronized (this) {
try {
mUseCount++;
mWaitCount++;
if (mWaitCount == 1) {
// Register the receiver for the first waiting request
registerConnectivityChangeReceiverLocked();
}
long waitMs = sNetworkAcquireTimeoutMs;
final long beginMs = SystemClock.elapsedRealtime();
do {
if (!isMobileDataEnabled()) {
// Fast fail if mobile data is not enabled
throw new MmsNetworkException("Mobile data is disabled");
}
// Always try to extend and check the MMS network connectivity
// before we start waiting to make sure we don't miss the change
// of MMS connectivity. As one example, some devices fail to send
// connectivity change intent. So this would make sure we catch
// the state change.
if (extendMmsConnectivityLocked()) {
// Connected
return;
}
try {
wait(Math.min(waitMs, NETWORK_ACQUIRE_WAIT_INTERVAL_MS));
} catch (final InterruptedException e) {
Log.w(MmsService.TAG, "Unexpected exception", e);
}
// Calculate the remaining time to wait
waitMs = sNetworkAcquireTimeoutMs - (SystemClock.elapsedRealtime() - beginMs);
} while (waitMs > 0);
// Last check
if (extendMmsConnectivityLocked()) {
return;
} else {
// Reaching here means timed out.
throw new MmsNetworkException("Acquiring MMS network timed out");
}
} finally {
mWaitCount--;
if (mWaitCount == 0) {
// Receiver is used to listen to connectivity change and unblock
// the waiting requests. If nobody's waiting on change, there is
// no need for the receiver. The auto extension timer will try
// to maintain the connectivity periodically.
unregisterConnectivityChangeReceiverLocked();
}
}
}
}
Вот мир кода, который я получил из исходного кода Android.
/**
* Run the MMS request.
*
* @param context the context to use
* @param networkManager the MmsNetworkManager to use to setup MMS network
* @param apnSettingsLoader the APN loader
* @param carrierConfigValuesLoader the carrier config loader
* @param userAgentInfoLoader the user agent info loader
*/
public void sendMms(final Context context, final MmsNetworkManager networkManager,
final ApnSettingsLoader apnSettingsLoader,
final CarrierConfigValuesLoader carrierConfigValuesLoader,
final UserAgentInfoLoader userAgentInfoLoader) {
Log.i(MmsService.TAG, "Execute " + this.getClass().getSimpleName());
int result = SmsManager.MMS_ERROR_UNSPECIFIED;
int httpStatusCode = 0;
byte[] response = null;
final Bundle mmsConfig = carrierConfigValuesLoader.get(MmsManager.DEFAULT_SUB_ID);
if (mmsConfig == null) {
Log.e(MmsService.TAG, "Failed to load carrier configuration values");
result = SmsManager.MMS_ERROR_CONFIGURATION_ERROR;
} else if (!loadRequest(context, mmsConfig)) {
Log.e(MmsService.TAG, "Failed to load PDU");
result = SmsManager.MMS_ERROR_IO_ERROR;
} else {
// Everything's OK. Now execute the request.
try {
// Acquire the MMS network
networkManager.acquireNetwork();
// Load the potential APNs. In most cases there should be only one APN available.
// On some devices on which we can't obtain APN from system, we look up our own
// APN list. Since we don't have exact information, we may get a list of potential
// APNs to try. Whenever we found a successful APN, we signal it and return.
final String apnName = networkManager.getApnName();
final List<ApnSettingsLoader.Apn> apns = apnSettingsLoader.get(apnName);
if (apns.size() < 1) {
throw new ApnException("No valid APN");
} else {
Log.d(MmsService.TAG, "Trying " + apns.size() + " APNs");
}
final String userAgent = userAgentInfoLoader.getUserAgent();
final String uaProfUrl = userAgentInfoLoader.getUAProfUrl();
MmsHttpException lastException = null;
for (ApnSettingsLoader.Apn apn : apns) {
Log.i(MmsService.TAG, "Using APN ["
+ "MMSC=" + apn.getMmsc() + ", "
+ "PROXY=" + apn.getMmsProxy() + ", "
+ "PORT=" + apn.getMmsProxyPort() + "]");
try {
final String url = getHttpRequestUrl(apn);
// Request a global route for the host to connect
requestRoute(networkManager.getConnectivityManager(), apn, url);
// Perform the HTTP request
response = doHttp(
context, networkManager, apn, mmsConfig, userAgent, uaProfUrl);
// Additional check of whether this is a success
if (isWrongApnResponse(response, mmsConfig)) {
throw new MmsHttpException(0/*statusCode*/, "Invalid sending address");
}
// Notify APN loader this is a valid APN
apn.setSuccess();
result = Activity.RESULT_OK;
break;
} catch (MmsHttpException e) {
Log.w(MmsService.TAG, "HTTP or network failure", e);
lastException = e;
}
}
if (lastException != null) {
throw lastException;
}
} catch (ApnException e) {
Log.e(MmsService.TAG, "MmsRequest: APN failure", e);
result = SmsManager.MMS_ERROR_INVALID_APN;
} catch (MmsNetworkException e) {
Log.e(MmsService.TAG, "MmsRequest: MMS network acquiring failure", e);
result = SmsManager.MMS_ERROR_UNABLE_CONNECT_MMS;
} catch (MmsHttpException e) {
Log.e(MmsService.TAG, "MmsRequest: HTTP or network I/O failure", e);
result = SmsManager.MMS_ERROR_HTTP_FAILURE;
httpStatusCode = e.getStatusCode();
} catch (Exception e) {
Log.e(MmsService.TAG, "MmsRequest: unexpected failure", e);
result = SmsManager.MMS_ERROR_UNSPECIFIED;
} finally {
// Release MMS network
networkManager.releaseNetwork();
}
}
// Process result and send back via PendingIntent
returnResult(context, result, response, httpStatusCode);
}
/**
* Request the route to the APN (either proxy host or the MMSC host)
*
* @param connectivityManager the ConnectivityManager to use
* @param apn the current APN
* @param url the URL to connect to
* @throws MmsHttpException for unknown host or route failure
*/
private static void requestRoute(final ConnectivityManager connectivityManager,
final ApnSettingsLoader.Apn apn, final String url) throws MmsHttpException {
String host = apn.getMmsProxy();
if (TextUtils.isEmpty(host)) {
final Uri uri = Uri.parse(url);
host = uri.getHost();
}
boolean success = false;
// Request route to all resolved host addresses
try {
for (final InetAddress addr : InetAddress.getAllByName(host)) {
final boolean requested = requestRouteToHostAddress(connectivityManager, addr);
if (requested) {
success = true;
Log.i(MmsService.TAG, "Requested route to " + addr);
} else {
Log.i(MmsService.TAG, "Could not requested route to " + addr);
}
}
if (!success) {
throw new MmsHttpException(0/*statusCode*/, "No route requested");
}
} catch (UnknownHostException e) {
Log.w(MmsService.TAG, "Unknown host " + host);
throw new MmsHttpException(0/*statusCode*/, "Unknown host");
}
}