Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.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
Java 用SIM2发送消息_Java_Android_Button_Sms - Fatal编程技术网

Java 用SIM2发送消息

Java 用SIM2发送消息,java,android,button,sms,Java,Android,Button,Sms,我在布局文件(mysend.xml)中放置了两个发送按钮: 使用SIM1发送(android:id=“@+id/send1”) 使用SIM2发送(android:id=“@+id/send2”) 我的java文件中的代码(MySend.java) 我的问题 如果我点击send1按钮,这是100%的工作,并用SIM1发送消息, 但是如果我点击send2按钮,消息将不会被发送 顺便说一句,很抱歉我的英语不好对于我来说,代码在sim1中运行良好,但在sim2中什么也没发生。查看正在引发的日志Nullp

我在布局文件(mysend.xml)中放置了两个发送按钮:

  • 使用SIM1发送(android:id=“@+id/send1”)
  • 使用SIM2发送(android:id=“@+id/send2”)
  • 我的java文件中的代码(MySend.java)

    我的问题

    如果我点击send1按钮,这是100%的工作,并用SIM1发送消息, 但是如果我点击send2按钮,消息将不会被发送


    顺便说一句,很抱歉我的英语不好

    对于我来说,代码在sim1中运行良好,但在sim2中什么也没发生。查看正在引发的日志Nullpointer异常。所以问题在于
    name=isms2
    。对于我的手机(芯片组展讯),对于sim1它是isms0,对于sim2它是isms1。您可以在dumpsys文件中检查您的文件,也可以在adb shell中使用以下代码进行检查:

    adb shell service list | grep isms
    
    这将列出设备中可用的ISM。对于我的设备,以下是输出:

    8       isms: [com.android.internal.telephony.ISms]
    12      isms1: [com.android.internal.telephony.ISms]
    16      isms0: [com.android.internal.telephony.ISms]
    
    请查看我的答案。

    @SuppressLint(“NewApi”)
    公共静态布尔sendSMS(上下文ctx、int-simID、字符串toNum、字符串centerNum、字符串smsText、pendingent sentinent、pendingent deliveryIntent){
    字符串名;
    试一试{
    System.out.println(“MODEL:+Build.MODEL”);
    if(simID==0){
    名称=Build.MODEL.equals(“飞利浦T939”)?“isms0”:“isms”//
    //适用于型号:“飞利浦T939”name=“isms0”
    }
    else if(simID==1){
    name=“isms2”;
    }
    否则{
    抛出新异常(“无法获取sim'+simID+'的服务,仅接受0,1作为值”);
    }           
    方法declaredMethod=Class.forName(“android.os.ServiceManager”).getDeclaredMethod(“getService”,新类[]{String.Class});
    declaredMethod.setAccessible(true);
    objectinvoke=declaredMethod.invoke(null,新对象[]{name});
    declaredMethod=Class.forName(“com.android.internal.telephony.ISms$Stub”).getDeclaredMethod(“asInterface”,新类[]{IBinder.Class});
    declaredMethod.setAccessible(true);
    invoke=declaredMethod.invoke(null,新对象[]{invoke});
    Log.e(“SimUtil”、“发送消息-”+smsText);
    如果(Build.VERSION.SDK_INT<18){
    invoke.getClass().getMethod(“sendText”,新类[]{String.Class,String.Class,String.Class,PendingIntent.Class,PendingIntent.Class})
    .invoke(invoke,新对象[]{toNum,centerNum,smsText,sentinent,deliveryIntent});
    }else if(SimUtil.getLolli(ctx)){
    ArrayList ArrayList=新的ArrayList();
    for(SubscriptionInfo subscriptionId:SubscriptionManager.from(ctx.getActiveSubscriptionInfo()){
    int subscriptionId2=subscriptionId.getSubscriptionId();
    add(Integer.valueOf(subscriptionId2));
    Log.e(“SimUtil”,“smsmsmanager-subscriptionId:”+subscriptionId2);
    }
    SmsManager.getSmsManagerForSubscriptionId(((整数)arrayList.get(simID)).intValue()
    .sendTextMessage(toNum、null、smsText、sentinent、deliveryIntent);
    }否则{
    SmsManager.getDefault().sendTextMessage(toNum、null、smsText、sentinent、deliveryIntent);
    }
    返回true;
    }catch(classnotfounde异常){
    Log.e(“apipas”,“ClassNotFoundException:+e.getMessage());
    e、 printStackTrace();
    Log.e(“SimUtil”,“错误1”);
    返回false;
    }捕获(无此方法例外){
    Log.e(“apipas”,“NoSuchMethodException:+e.getMessage());
    Log.e(“SimUtil”,“错误2”);
    e、 printStackTrace();
    返回false;
    }捕获(调用TargetException e){
    Log.e(“apipas”,“InvocationTargetException:”+e.getMessage());
    Log.e(“SimUtil”,“错误3”);
    e、 printStackTrace();
    返回false;
    }捕获(非法访问例外e){
    Log.e(“apipas”,“IllegalAccessException:+e.getMessage());
    Log.e(“SimUtil”,“错误4”);
    e、 printStackTrace();
    返回false;
    }捕获(例外e){
    Log.e(“apipas”,“Exception:+e.getMessage());
    Log.e(“SimUtil”,“错误5”);
    e、 printStackTrace();
    返回false;
    }
    }
    私有静态布尔lollypopPlus(上下文){
    如果(Build.VERSION.SDK_INT<22){
    返回false;
    }
    if(SubscriptionManager.from(context.getActiveSubscriptionInfo()>1){
    返回true;
    }
    返回false;
    }
    
    我提供的解决方案太接近您使用的@,您通过SIM2发送时得到的输出是什么?您的手机型号是什么?记录以下列表会很有帮助:adb外壳服务列表
    adb shell service list | grep isms
    
    8       isms: [com.android.internal.telephony.ISms]
    12      isms1: [com.android.internal.telephony.ISms]
    16      isms0: [com.android.internal.telephony.ISms]
    
     @SuppressLint("NewApi")
    public static boolean sendSMS(Context ctx, int simID, String toNum, String centerNum, String smsText, PendingIntent sentIntent, PendingIntent deliveryIntent) {
        String name;
    
        try {
            System.out.println("MODEL:"+Build.MODEL);
            if (simID == 0) {
                name = Build.MODEL.equals("Philips T939") ? "isms0" : "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 declaredMethod = Class.forName("android.os.ServiceManager").getDeclaredMethod("getService", new Class[]{String.class});
            declaredMethod.setAccessible(true);
            Object invoke = declaredMethod.invoke(null, new Object[]{name});
            declaredMethod = Class.forName("com.android.internal.telephony.ISms$Stub").getDeclaredMethod("asInterface", new Class[]{IBinder.class});
            declaredMethod.setAccessible(true);
            invoke = declaredMethod.invoke(null, new Object[]{invoke});
            Log.e("SimUtil", "send msg - " + smsText);
            if (Build.VERSION.SDK_INT < 18) {
                invoke.getClass().getMethod("sendText", new Class[]{String.class, String.class, String.class, PendingIntent.class, PendingIntent.class})
                        .invoke(invoke, new Object[]{toNum, centerNum, smsText, sentIntent, deliveryIntent});
            } else if (SimUtil.getLolli(ctx)) {
                ArrayList arrayList = new ArrayList();
                for (SubscriptionInfo subscriptionId : SubscriptionManager.from(ctx).getActiveSubscriptionInfoList()) {
                    int subscriptionId2 = subscriptionId.getSubscriptionId();
                    arrayList.add(Integer.valueOf(subscriptionId2));
                    Log.e("SimUtil", "SmsManager - subscriptionId: " + subscriptionId2);
                }
                SmsManager.getSmsManagerForSubscriptionId(((Integer) arrayList.get(simID)).intValue())
                        .sendTextMessage(toNum, null, smsText, sentIntent, deliveryIntent);
            } else {
                SmsManager.getDefault().sendTextMessage(toNum, null, smsText, sentIntent, deliveryIntent);
            }
            return true;
        } catch (ClassNotFoundException e) {
            Log.e("apipas", "ClassNotFoundException:" + e.getMessage());
            e.printStackTrace();
            Log.e("SimUtil", "error 1");
            return false;
        } catch (NoSuchMethodException e) {
            Log.e("apipas", "NoSuchMethodException:" + e.getMessage());
            Log.e("SimUtil", "error 2");
            e.printStackTrace();
            return false;
        } catch (InvocationTargetException e) {
            Log.e("apipas", "InvocationTargetException:" + e.getMessage());
            Log.e("SimUtil", "error 3");
            e.printStackTrace();
            return false;
        } catch (IllegalAccessException e) {
            Log.e("apipas", "IllegalAccessException:" + e.getMessage());
            Log.e("SimUtil", "error 4");
            e.printStackTrace();
            return false;
        } catch (Exception e) {
            Log.e("apipas", "Exception:" + e.getMessage());
            Log.e("SimUtil", "error 5");
            e.printStackTrace();
            return false;
        }
    }
    
    private static boolean lollypopPlus(Context context) {
        if (Build.VERSION.SDK_INT < 22) {
            return false;
        }
        if (SubscriptionManager.from(context).getActiveSubscriptionInfoCount() > 1) {
            return true;
        }
        return false;
    }