Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:如何在双卡手机中使用特定SIM卡发送短信?_Android_Sms_Dual Sim - Fatal编程技术网

Android:如何在双卡手机中使用特定SIM卡发送短信?

Android:如何在双卡手机中使用特定SIM卡发送短信?,android,sms,dual-sim,Android,Sms,Dual Sim,我发现有一些方法可以这样做,但它给了我一个异常(尝试在空对象引用上调用虚拟方法“java.lang.Class java.lang.Object.getClass()”。我正在用我的Moto G第三代进行测试。下面是代码,如果我遗漏了任何内容,请告诉我,或者有其他可用的方法。多谢各位 import android.app.PendingIntent; import android.content.Context; import android.os.IBinder; import android

我发现有一些方法可以这样做,但它给了我一个异常(尝试在空对象引用上调用虚拟方法“java.lang.Class java.lang.Object.getClass()”。我正在用我的Moto G第三代进行测试。下面是代码,如果我遗漏了任何内容,请告诉我,或者有其他可用的方法。多谢各位

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;
    }

如果您的目标是API 22或更高。您可以使用以下代码获取SIM卡列表。您还可以允许用户选择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);
}

如果您的目标是API 22或更高。您可以使用以下代码获取SIM卡列表。您还可以允许用户选择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);
}

如果您的目标是Android API 22或更高版本,Android支持双sim卡设备@shr3jn你能给我举个例子吗?我已经回答了你的问题。如果你的目标是Android API 22或更高版本,Android支持双sim设备@shr3jn你能给我举个例子吗?我已经回答了你的问题。