简体   繁体   English

Android:如何在双SIM卡手机中发送带有特定SIM卡的短信?

[英]Android: How to send sms with particular SIM in dual sim mobiles?

I found some code to do this, but It's giving me an exception ( Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference). 我找到了一些执行此操作的代码 ,但这给了我一个例外(尝试在空对象引用上调用虚拟方法'java.lang.Class java.lang.Object.getClass()')。 I am testing with my Moto G 3rd Gen. Below is the code, Please let me know if I am missing anything or any other approach are available. 我正在用Moto G第三代进行测试。以下是代码,如果我缺少任何东西或任何其他可用方法,请告诉我。 Thank you. 谢谢。

import android.app.PendingIntent;
import android.content.Context;
import android.os.IBinder;
import android.util.Log;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


public class SimUtil {

    private static final String TAG = "SimUtil";

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

       SimUtil cls = new SimUtil();

        String name;

        try {
            if (simID == 0) {
                name = "isms";

            } else if (simID == 1) {
                name = "isms2";

            } else {
                throw new Exception("can not get service which for sim '" + simID + "', only 0,1 accepted as values");
            }

            try
            {
                Method method = Class.forName("android.os.ServiceManager").getDeclaredMethod("getService", new Class[]{String.class});
                method.setAccessible(true);
                Object param = method.invoke(null, new Object[]{name});

                if (param == null)
                {
                    throw new RuntimeException("can not get service which is named '" + name + "'");
                }
                method = Class.forName("com.android.internal.telephony.ISms$Stub").getDeclaredMethod("asInterface", new Class[]{IBinder.class});
                method.setAccessible(true);
                Object stubObj = method.invoke(null, new Object[]{param});

                if (stubObj == null)
                {
                    throw new RuntimeException("can not get telephony which is named '" + name + "'");
                }


                method = stubObj.getClass().getMethod("sendText", String.class, String.class, String.class, String.class, PendingIntent.class, PendingIntent.class);

                method.invoke(null, new Object[]{ctx.getPackageName(), toNum, null, smsText, sentIntent, deliveryIntent});


            } catch (ClassNotFoundException e)
            {
                throw new RuntimeException(e);
            } catch (NoSuchMethodException e)
            {
                throw new RuntimeException(e);
            } catch (InvocationTargetException e)
            {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e)
            {
                throw new RuntimeException(e);
            }

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

If you're targeting API 22 or higher. 如果您要定位API 22或更高版本。 You can use the following code to get the list of SIM Cards. 您可以使用以下代码获取SIM卡列表。 You can also allow user to choose the SIM Card: 您还可以允许用户选择SIM卡:

final ArrayList<Integer> simCardList = new ArrayList<>();
SubscriptionManager subscriptionManager;
subscriptionManager = SubscriptionManager.from(activity);
final List<SubscriptionInfo> subscriptionInfoList = subscriptionManager
                    .getActiveSubscriptionInfoList();
for (SubscriptionInfo subscriptionInfo : subscriptionInfoList) {
    int subscriptionId = subscriptionInfo.getSubscriptionId();
    simCardList.add(subscriptionId);
}

Finally send the SMS using following code: 最后使用以下代码发送SMS:

int smsToSendFrom = simCardList.get(0); //assign your desired sim to send sms, or user selected choice
SmsManager.getSmsManagerForSubscriptionId(smsToSendFrom)
                            .sendTextMessage(phoneNumber, null, msg, sentPI, deliveredPI); //use your phone number, message and pending intents

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM