0

我正在使用 Kotlin 构建一个 Android 消息传递应用程序。我的应用程序将在应用程序内发送彩信或照片消息。它不应该使用内置服务发送。我正在尝试使用此库https://github.com/klinker41/android-smsmms?fbclid=IwAR2lFynDGHsbExnzdhn1fIPX-m55lZxoDJjwwpdL4jgcANP1-8mdjKSFe_8来实现逻辑。但它没有按预期工作。

这就是我发送彩信的方式。

我使用以下功能初始化了设置。

private fun initApns() {
        ApnUtils.initDefaultApns(
            this
        ) {
            settings =
                Settings.get(this@SendOrdinaryMessageActivity, true)
        }
    }

    private fun initSettings() {
        settings = Settings[this]
        if (TextUtils.isEmpty(settings!!.mmsc) &&
            Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP
        ) {
            initApns()
        }
    }

当活动开始时,我调用 initSettings 函数。

这是我的 ApnUtils 课程。

public class ApnUtils {
    private static final String TAG = "ApnUtils";

    public ApnUtils() {
    }

    public static void initDefaultApns(final Context context, final ApnUtils.OnApnFinishedListener listener) {
        loadMmsSettings(context);
        final ArrayList<ApnUtils.APN> apns = loadApns(context);
        if (apns != null && apns.size() != 0) {
            if (apns.size() == 1) {
                setApns(context, (ApnUtils.APN)apns.get(0));
                if (listener != null) {
                    listener.onFinished();
                }
            } else if (!PreferenceManager.getDefaultSharedPreferences(context).getBoolean("has_seen_select_apns_warning", false)) {
                (new Builder(context)).setTitle(string.auto_select_apn).setMessage(string.auto_select_multiple_apns).setPositiveButton(string.ok, new OnClickListener() {
                    public void onClick(DialogInterface dialogInterface, int num) {
                        ApnUtils.showApnChooser(context, apns, listener);
                    }
                }).show();
                PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean("has_seen_select_apns_warning", true).commit();
            } else {
                showApnChooser(context, apns, listener);
            }
        } else {
            Log.v("ApnUtils", "Found no APNs :( Damn CDMA network probably.");
            Toast.makeText(context, context.getString(string.auto_select_failed), 0).show();
            if (listener != null) {
                listener.onFinished();
            }
        }

    }

    private static void showApnChooser(final Context context, final ArrayList<ApnUtils.APN> apns, final ApnUtils.OnApnFinishedListener listener) {
        CharSequence[] items = new CharSequence[apns.size()];
        SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
        String curMmsc = sharedPrefs.getString("mmsc_url", "");
        String curProxy = sharedPrefs.getString("mms_proxy", "");
        String curPort = sharedPrefs.getString("mms_port", "");
        int defaultApn = -1;

        for(int i = 0; i < items.length; ++i) {
            ApnUtils.APN apn = (ApnUtils.APN)apns.get(i);
            items[i] = i + 1 + ". " + apn.name;
            if (apn.mmsc.equals(curMmsc) && apn.proxy.equals(curProxy) && apn.port.equals(curPort)) {
                defaultApn = i;
            }
        }

        (new Builder(context)).setSingleChoiceItems(items, defaultApn, new OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int i) {
                ApnUtils.setApns(context, (ApnUtils.APN)apns.get(i));
                if (listener != null) {
                    listener.onFinished();
                }

                dialogInterface.dismiss();
            }
        }).setOnCancelListener(new OnCancelListener() {
            public void onCancel(DialogInterface dialogInterface) {
                if (listener != null) {
                    listener.onFinished();
                }

            }
        }).show();
    }

    private static void loadMmsSettings(Context context) {
        XmlResourceParser parser = context.getResources().getXml(xml.mms_config);
        String maxMessageSize = "1000000";
        int maxImageHeight = 800;
        int maxImageWidth = 800;
        String userAgent = "Android Messaging";
        String uaProfUrl = "http://www.gstatic.com/android/hangouts/hangouts_mms_ua_profile.xml";

        String tag;
        String name;
        try {
            beginDocument(parser, "mms_config");

            while(true) {
                nextElement(parser);
                tag = parser.getName();
                if (tag == null) {
                    break;
                }

                name = parser.getAttributeName(0);
                String value = parser.getAttributeValue(0);
                String text = null;
                if (parser.next() == 4) {
                    text = parser.getText();
                }

                Log.v("ApnUtils", "tag: " + tag + " value: " + value + " - " + text);
                if ("name".equalsIgnoreCase(name)) {
                    if ("int".equals(tag)) {
                        if ("maxMessageSize".equalsIgnoreCase(value)) {
                            maxMessageSize = text;
                        } else if ("maxImageHeight".equalsIgnoreCase(value)) {
                            maxImageHeight = Integer.parseInt(text);
                        } else if ("maxImageWidth".equalsIgnoreCase(value)) {
                            maxImageWidth = Integer.parseInt(text);
                        }
                    } else if ("string".equals(tag)) {
                        if ("userAgent".equalsIgnoreCase(value)) {
                            userAgent = text;
                        } else if ("uaProfUrl".equalsIgnoreCase(value)) {
                            uaProfUrl = text;
                        }
                    }
                }
            }
        } catch (XmlPullParserException var16) {
            Log.e("ApnUtils", "loadMmsSettings caught ", var16);
        } catch (NumberFormatException var17) {
            Log.e("ApnUtils", "loadMmsSettings caught ", var17);
        } catch (IOException var18) {
            Log.e("ApnUtils", "loadMmsSettings caught ", var18);
        } finally {
            parser.close();
        }

        tag = null;
        if (tag != null) {
            name = String.format("MmsConfig.loadMmsSettings mms_config.xml missing %s setting", tag);
            Log.e("ApnUtils", name);
        }

        if (VERSION.SDK_INT >= 19) {
            TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService("phone");
            userAgent = telephonyManager.getMmsUserAgent();
            uaProfUrl = telephonyManager.getMmsUAProfUrl();
        }

        PreferenceManager.getDefaultSharedPreferences(context).edit().putInt("mms_max_width", maxImageWidth).putInt("mms_max_height", maxImageHeight).putString("mms_max_size", maxMessageSize).putString("mms_agent", userAgent).putString("mms_user_agent_profile_url", uaProfUrl).commit();
    }

    private static ArrayList<ApnUtils.APN> loadApns(Context context) {
        XmlResourceParser parser = context.getResources().getXml(xml.apns);
        ArrayList<ApnUtils.APN> apns = new ArrayList();
        String mmsc = "";
        String proxy = "";
        String port = "";
        String carrier = "";
        int mcc = true;
        int mnc = true;
        TelephonyManager manager = (TelephonyManager)context.getSystemService("phone");
        String networkOperator = manager.getNetworkOperator();
        String errorStr;
        int mcc;
        int mnc;
        if (isValidNetworkOperator(networkOperator)) {
            mcc = Integer.parseInt(networkOperator.substring(0, 3));
            errorStr = networkOperator.substring(3);

            try {
                mnc = Integer.parseInt(errorStr.replaceFirst("^0{1,2}", ""));
            } catch (Exception var27) {
                mnc = -1;
            }
        } else {
            mcc = context.getResources().getConfiguration().mcc;
            mnc = context.getResources().getConfiguration().mnc;
        }

        try {
            if (mcc == -1) {
                mcc = Integer.parseInt((new ServiceState()).getOperatorNumeric().substring(0, 3));
            }

            if (mnc == -1) {
                TelephonyManager tm = (TelephonyManager)context.getSystemService("phone");
                mnc = ((CdmaCellLocation)tm.getCellLocation()).getSystemId();
            }
        } catch (Exception var26) {
        }

        if (mcc != -1 && mnc != -1) {
            Log.v("ApnUtils", "mcc: " + mcc + " mnc: " + mnc);

            try {
                beginDocument(parser, "apns");

                label301:
                while(true) {
                    int i;
                    do {
                        boolean mccCorrect;
                        boolean mncCorrect;
                        int i;
                        String name;
                        do {
                            do {
                                nextElement(parser);
                                errorStr = parser.getName();
                                if (errorStr == null) {
                                    break label301;
                                }

                                mccCorrect = false;
                                mncCorrect = false;

                                for(i = 0; i < parser.getAttributeCount(); ++i) {
                                    try {
                                        name = parser.getAttributeName(i);
                                        i = Integer.parseInt(parser.getAttributeValue(i));
                                        if ("mcc".equals(name) && mcc == i) {
                                            mccCorrect = true;
                                        } else if ("mnc".equals(name) && mnc == i) {
                                            mncCorrect = true;
                                        }
                                    } catch (Exception var28) {
                                    }
                                }
                            } while(!mccCorrect);
                        } while(!mncCorrect);

                        for(i = 0; i < parser.getAttributeCount(); ++i) {
                            name = parser.getAttributeName(i);
                            String value = parser.getAttributeValue(i);
                            if ("type".equals(name)) {
                                if (!value.contains("mms")) {
                                    mmsc = "";
                                    proxy = "";
                                    port = "";
                                    break;
                                }
                            } else if ("mmsc".equals(name)) {
                                mmsc = value;
                            } else if ("mmsproxy".equals(name)) {
                                proxy = value;
                            } else if ("mmsport".equals(name)) {
                                port = value;
                            } else if ("carrier".equals(name)) {
                                carrier = value;
                            } else if ("port".equals(name) && port.equals("")) {
                                port = value;
                            }
                        }
                    } while(mmsc.equals(""));

                    ApnUtils.APN apn = new ApnUtils.APN();
                    apn.name = carrier;
                    apn.mmsc = mmsc;
                    apn.proxy = proxy;
                    apn.port = port;
                    boolean contains = false;

                    for(i = 0; i < apns.size(); ++i) {
                        ApnUtils.APN current = (ApnUtils.APN)apns.get(i);
                        if (current.mmsc.equals(apn.mmsc) && current.port.equals(apn.port) && current.proxy.equals(apn.proxy)) {
                            contains = true;
                            break;
                        }
                    }

                    if (!contains) {
                        apns.add(apn);
                    }
                }
            } catch (XmlPullParserException var29) {
                Log.e("ApnUtils", "loadApns caught ", var29);
            } catch (NumberFormatException var30) {
                Log.e("ApnUtils", "loadApns caught ", var30);
            } catch (IOException var31) {
                Log.e("ApnUtils", "loadApns caught ", var31);
            } finally {
                parser.close();
            }

            Log.v("ApnUtils", "MMSC: " + mmsc + ", MMS Proxy: " + proxy + ", MMS Port: " + port);
            errorStr = null;
            if (errorStr != null) {
                String err = String.format("MmsConfig.loadMmsSettings mms_config.xml missing %s setting", errorStr);
                Log.e("ApnUtils", err);
            }

            return apns;
        } else {
            Log.v("ApnUtils", "couldn't find both mcc and mnc. mcc = " + mcc + ", mnc = " + mnc);
            return null;
        }
    }

    private static boolean isValidNetworkOperator(String networkOperator) {
        return networkOperator != null && !networkOperator.isEmpty() && !networkOperator.contains("null");
    }

    private static void setApns(Context context, ApnUtils.APN apn) {
        PreferenceManager.getDefaultSharedPreferences(context).edit().putString("mmsc_url", apn.mmsc).putString("mms_proxy", apn.proxy).putString("mms_port", apn.port).commit();
    }

    private static void beginDocument(XmlPullParser parser, String firstElementName) throws XmlPullParserException, IOException {
        int type;
        while((type = parser.next()) != 2 && type != 1) {
        }

        if (type != 2) {
            throw new XmlPullParserException("No start tag found");
        } else if (!parser.getName().equals(firstElementName)) {
            throw new XmlPullParserException("Unexpected start tag: found " + parser.getName() + ", expected " + firstElementName);
        }
    }

    private static void nextElement(XmlPullParser parser) throws XmlPullParserException, IOException {
        int type;
        while((type = parser.next()) != 2 && type != 1) {
        }

    }

    private static class APN {
        public String name;
        public String mmsc;
        public String proxy;
        public String port;

        private APN() {
        }
    }

    public interface OnApnFinishedListener {
        void onFinished();
    }
}

这是我发送带有照片的彩信的功能。

fun sendMMS(
    to: String,
    messageText: String,
    settings: com.forkthecoop.com22222.messaging.Settings
) {
    val sendSettings = Settings()
    //Telephony.Carriers.TYPE
    sendSettings.mmsc =  settings.mmsc
    sendSettings.proxy = settings.mmsProxy
    sendSettings.port = settings.mmsPort
    sendSettings.useSystemSending = true
    var sendTransaction = Transaction(context, sendSettings)
    var message = Message(messageText, to)
     message.setImage(this.bitmap)
    sendTransaction.sendNewMessage(message, Transaction.NO_THREAD_ID)
    Toast.makeText(context, "Message sent", Toast.LENGTH_SHORT).show()
}

当我运行代码时,它正在烤消息“已发送消息”,但它没有向其他设备发送任何消息。我的代码有什么问题,我该如何解决?

4

0 回答 0