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

编程入门 行业动态 更新时间:2024-10-24 15:22:10
本文介绍了Android:如何在双卡手机上发送带有特定SIM卡的短信?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我找到了一些code可以做到这一点,但它给了我一个异常(试图在空对象引用上调用虚拟方法‘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); }

最终使用以下代码发送短信:

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

更多推荐

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

本文发布于:2023-11-28 06:40:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641331.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:机上   短信   如何在   Android   SIM

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!