簡體   English   中英

在 Android 上以編程方式發送彩信

[英]Sending MMS programmatically on Android

我有一個我應該做的任務有問題,我應該在 android 2.1 上使用我們自己的界面發送彩信,所以你可以猜到調用默認活動是不可能的。 所以我的問題是,有沒有辦法使用 android SDK 以編程方式發送彩信而不調用他們的意圖,我嘗試在 eclipse 中導入彩信應用程序,但大多數類都是 com.google.android,這意味着它們不是開源的,所以我如果可能的話,不知道如何獲得它們,或者如何模仿它們。 我什至想使用反射從 Dalvik 加載它們,但我認為這是最后的努力,可能不會帶來結果。

有什么想法嗎?

順便說一句,我發現

如何在Android中通過彩信發送圖像?

將彩信發送到不同的 Android 設備

但它們不起作用..(沒有專有類)

雖然這個問題有一段時間沒有回答,但我當時找到了一個方法,只是忘記發帖了。 但是我得到了原始的 MMS 應用程序並削弱了二進制類並添加了相應的要求來完成,因為它們中的大多數是構建系統私有的。 在 android 中制作 mms 發送器的唯一方法(我知道)是使用源代碼樹構建應用程序。 通過這種方式,您將可以訪問受限制的 MMS 功能,並且通常非常容易,因為源中有一個 MMSManager,它本身就是一個 MMSManager,盡管這在 sdk 中不是公開的。 我知道我的回答有點含糊,但是對於那些沿着這條路走的人.. 為路上的一些顛簸做好准備.. :)

像 SmsManager 一樣,有類似 MmsManager api 的東西,它沒有被 android 框架公開。

因此,要先發送彩信,您必須先獲取彩信網絡,然后才能進行 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");
    }
}
 private void sendSMS(String phoneNumber, String message)
    {        
        PendingIntent pi = PendingIntent.getActivity(this, 0,
            new Intent(this, SMS.class), 0);                
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, pi, null);        
    }    
}

試試這個

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM