Send sms in pdu mode in Android

5.5k views Asked by At

I am new in Android. I want to send a sms in pdu mode and i found this code, but some class doesn't Support in android studio (Bold Lines). Can anyone help me ?

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

There are 1 answers

0
Choubdar On BEST ANSWER

I found my Answer. More About this you can read in 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();
    }