gpt4 book ai didi

android - 在 Android 中以 pdu 模式发送短信

转载 作者:行者123 更新时间:2023-11-30 02:49:28 27 4
gpt4 key购买 nike

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

关于android - 在 Android 中以 pdu 模式发送短信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24464237/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com