且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何选择主SIM卡双卡支持电池发短信?

更新时间:2023-01-06 12:27:23

请TelePhonyInfo.java的对象,它是telephonyInfo这样。 TelephonyInfo telephonyInfo = TelephonyInfo.getInstance(getApplicationContext());

Make Object of TelePhonyInfo.java which is telephonyInfo like this. TelephonyInfo telephonyInfo=TelephonyInfo.getInstance(getApplicationContext());

使用下面的函数然后检查SIM卡设置:

Then check sim settings using following function:

public int  getDefaultSimmm(Context context) {

    Object tm = context.getSystemService(Context.TELEPHONY_SERVICE);
    Method method_getDefaultSim;
    int defaultSimm = -1;
    try {
        method_getDefaultSim = tm.getClass().getDeclaredMethod("getDefaultSim");
        method_getDefaultSim.setAccessible(true);
        defaultSimm = (Integer) method_getDefaultSim.invoke(tm);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Method method_getSmsDefaultSim;
    int smsDefaultSim = -1;
    try {
        method_getSmsDefaultSim = tm.getClass().getDeclaredMethod("getSmsDefaultSim");
        smsDefaultSim = (Integer) method_getSmsDefaultSim.invoke(tm);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return smsDefaultSim;
    }

基于defaultSim整型变量,如果0或1。发送您所选择的短信从SIM卡使用此功能

based on defaultSim integer variable if 0 or 1. Send sms from SIM of your choice using this function:

public boolean sendSMS(Context ctx, int simID, String toNum, String centerNum, String smsText, PendingIntent sentIntent, PendingIntent deliveryIntent) {
        String name;

        try {
            if (simID == 0) {
                name = "isms";
                // for model : "Philips T939" name = "isms0"
            } else if (simID == 1) {
                name = "isms2";
            } else {
                throw new Exception("can not get service which for sim '" + simID + "', only 0,1 accepted as values");
            }
            Method method = Class.forName("android.os.ServiceManager").getDeclaredMethod("getService", String.class);
            method.setAccessible(true);
            Object param = method.invoke(null, name);

            method = Class.forName("com.android.internal.telephony.ISms$Stub").getDeclaredMethod("asInterface", IBinder.class);
            method.setAccessible(true);
            Object stubObj = method.invoke(null, param);
            if (Build.VERSION.SDK_INT < 18) {
                method = stubObj.getClass().getMethod("sendText", String.class, String.class, String.class, PendingIntent.class, PendingIntent.class);
                method.invoke(stubObj, toNum, centerNum, smsText, sentIntent, deliveryIntent);
            } else {
                method = stubObj.getClass().getMethod("sendText", String.class, String.class, String.class, String.class, PendingIntent.class, PendingIntent.class);
                method.invoke(stubObj, ctx.getPackageName(), toNum, centerNum, smsText, sentIntent, deliveryIntent);
            }

            return true;
        } catch (ClassNotFoundException e) {
            Log.e("apipas", "ClassNotFoundException:" + e.getMessage());
        } catch (NoSuchMethodException e) {
            Log.e("apipas", "NoSuchMethodException:" + e.getMessage());
        } catch (InvocationTargetException e) {
            Log.e("apipas", "InvocationTargetException:" + e.getMessage());
        } catch (IllegalAccessException e) {
            Log.e("apipas", "IllegalAccessException:" + e.getMessage());
        } catch (Exception e) {
            Log.e("apipas", "Exception:" + e.getMessage());
        }
        return false;
    }