MultiSmsSender/src/com/openwide/android/MySMSManager.java

78 lines
2.6 KiB
Java
Raw Normal View History

package com.openwide.android;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import android.app.PendingIntent;
import android.os.Build;
import android.util.Log;
public class MySMSManager {
private static Object manager;
private static Method getDefault;
private static Method divide;
private static Method sendMutlipart;
public MySMSManager() {
if(manager == null) {
String className;
int sdkVersion = Integer.parseInt(Build.VERSION.SDK);
if (sdkVersion <= 3) { //CUPCAKE
Log.d("MultiSMSSender", "Cupcake version");
className = "android.telephony.gsm.SmsManager";
} else {
Log.d("MultiSMSSender", "version > Cupcake");
className = "android.telephony.SmsManager";
}
try {
Class clazz =
Class.forName(className);
Object[] paramInvoke = new Object[0];
Class[] signGetDefault = new Class[]{};
Class<String>[] signDivide = new Class[] {String.class};
Class[] signSend = new Class[] {String.class, String.class, ArrayList.class, ArrayList.class, ArrayList.class};
getDefault = clazz.getMethod("getDefault", signGetDefault);
divide = clazz.getMethod("divideMessage", signDivide);
sendMutlipart = clazz.getMethod("sendMultipartTextMessage", signSend);
manager = getDefault.invoke(null, paramInvoke);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public ArrayList<String> divideMessage(String text){
Object[] param = new Object[1];
param[0] = text;
try {
return (ArrayList<String>)divide.invoke(manager, param);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return null;
}
public void sendMultipartTextMessage(String destinationAddress, String scAddress, ArrayList<String> parts, ArrayList<PendingIntent> sentIntents, ArrayList<PendingIntent> deliveryIntents ) {
Object[] param = new Object[5];
param[0] = destinationAddress;
param[1] = scAddress;
param[2] = parts;
param[3] = sentIntents;
param[4] = deliveryIntents;
try {
sendMutlipart.invoke(manager, param);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}