在Android中以pdu模式发送短信

时间:2014-06-28 06:23:40

标签: android android-studio smsmanager smspdu

我是Android新手。 我想在pdu模式下发送短信,我发现这个代码,但有些类不支持android studio(Bold Lines)。任何人都可以帮助我吗?

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();
    }
}

1 个答案:

答案 0 :(得分:1)

我找到了答案。有关这方面的更多信息,请阅读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();
    }