以PDU模式发送短信

以PDU模式发送短信

我是Android新手。
我想以pdu模式发送短信,但我找到了此代码,但是android studio中不支持某些类(粗体)。谁能帮我 ?

private void sendsmsBypdu(String phoneNumber,String message) {

    int size;
    Field f;

    Log.d(TAG,"Retrieving phone instance ...");
    ContactsContract.CommonDataKinds.Phone phone = **PhoneFactory.getDefaultPhone();**

    /* Get IccSmsInterfaceManager */
    Log.d(TAG,"Retrieving SmsInterfaceManager ...");
    **IccSmsInterfaceManager ismsm = getIccSmsInterfaceManager();**

    try {
        Log.d(TAG,"Retrieving mDispatcher ...");
        **f = IccSmsInterfaceManager.class.getDeclaredField("mDispatcher");**
        f.setAccessible(true);
        SmsManager sms_smg = (SmsManager)f.get(ismsm);

        Log.d(TAG, "Formatting class 0 SMS ...");
        byte[] b = new byte[0];
        SmsMessage.SubmitPdu pdus = SmsMessage.getSubmitPdu(null, phoneNumber, message,false);

            /* change class to Class 0 */
        size = (int)pdus.encodedMessage[2];
        size = (size/2) + (size%2);
        pdus.encodedMessage[size+5] = (byte)0xF0;

            /* send raw pdu */
        Log.d(TAG,"Sending SMS via sendRawPdu() ...");
        try
        {
                /* Android 2.2 -> 4.0.* */
            Method m = SmsManager.class.getDeclaredMethod("sendRawPdu", b.getClass(), b.getClass(), PendingIntent.class, PendingIntent.class);
            m.setAccessible(true);
            m.invoke(sms_smg, pdus.encodedScAddress, pdus.encodedMessage, null, null);
        }
        catch(NoSuchMethodException e)
        {
                /* Android 4.1.2 */
            Method m = SmsManager.class.getDeclaredMethod("sendRawPdu", b.getClass(), b.getClass(), PendingIntent.class, PendingIntent.class, String.class);
            m.setAccessible(true);
            m.invoke(sms_smg, pdus.encodedScAddress, pdus.encodedMessage, null, null, phoneNumber);
        }
        Log.d(TAG, "SMS sent");

    } catch (SecurityException e) {
        Log.e(TAG, "Exception: Security !");
        e.printStackTrace();
    }
    catch (NoSuchFieldException e) {
        Log.e(TAG, "Exception: Field mDispatcher not found !");
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        Log.e(TAG, "Exception: Illegal Argument !");
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        Log.e(TAG, "Exception: Illegal access !");
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        Log.e(TAG, "Exception: sendRawPdu() not found !");
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        Log.e(TAG, "Exception: cannot invoke sendRawPdu() !");
        e.printStackTrace();
    }
}

最佳答案

我找到了答案。关于此的更多信息,您可以在Hush SMS中阅读

        SmsManager sm = SmsManager.getDefault();

        // Get method "sendRawPdu"
        byte[] bb = new byte[1];
        SmsMessage.SubmitPdu mypdu = SmsMessage.getSubmitPdu(null, pNo, msg, true);
        size = (int) mypdu.encodedMessage[2];
        size = (size / 2) + (size % 2);


        mypdu.encodedMessage[size + 5] = (byte) 0xF0;



        Log.d(TAG, dumpHexString(mypdu.encodedMessage, 0, mypdu.encodedMessage.toString().length()));


        Method m2 = SmsManager.class.getDeclaredMethod("sendRawPdu", bb.getClass(),bb.getClass(),PendingIntent.class,PendingIntent.class,boolean.class,boolean.class);
        Log.d("success", "success getting sendRawPdu");

        m2.setAccessible(true);

        int length = msg.length();
        count = length / 160;
        int m = length % 160;
        if (m != 0) {
            count++;
        }


        m2.invoke(sm, mypdu.encodedScAddress,mypdu.encodedMessage,sentPI,deliveredPI, Boolean.valueOf(true),Boolean.valueOf(true));
        Log.d("success", "success sending message");


    } catch (SecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

    } catch (NoSuchMethodException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

    } catch (InvocationTargetException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

10-08 07:10