- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我正在使用短信管理器发送短信。对于单 SIM 卡,它可以完美地发送短信。但在双 SIM 卡中,短信不会发送。是否可以从双 SIM 卡发送短信,如果可能意味着我该如何选择发送短信的 SIM 卡。谁能知道帮我解决这个问题。
单 SIM 卡工作代码
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(ph_number, null, body, null,null);
最佳答案
我使用这种方式来管理用于发送 SMS 甚至长消息的 sim 卡。它在我的双卡手机联想 A319 (4.4.3) 上工作,无需 root。它建立在反射之上。
import android.app.PendingIntent;
import android.content.Context;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Apipas on 6/4/15.
*/
public class SimUtil {
public static 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;
}
public static boolean sendMultipartTextSMS(Context ctx, int simID, String toNum, String centerNum, ArrayList<String> smsTextlist, ArrayList<PendingIntent> sentIntentList, ArrayList<PendingIntent> deliveryIntentList) {
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("sendMultipartText", String.class, String.class, List.class, List.class, List.class);
method.invoke(stubObj, toNum, centerNum, smsTextlist, sentIntentList, deliveryIntentList);
} else {
method = stubObj.getClass().getMethod("sendMultipartText", String.class, String.class, String.class, List.class, List.class, List.class);
method.invoke(stubObj, ctx.getPackageName(), toNum, centerNum, smsTextlist, sentIntentList, deliveryIntentList);
}
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;
}
}
添加权限:
<uses-permission android:name="android.permission.SEND_SMS"/>
然后像这样调用那个(血腥的)静态方法:)
使用 SIM1:
SimUtil.sendSMS(this,0,"00970XXXXXXXXX",null,"Hi Stackoverflow! its me Maher. Sent by sim1",null,null);
使用 SIM2:
SimUtil.sendSMS(this,1,"00970XXXXXXXXX",null,"Hi Stackoverflow! its me Maher. Sent by sim2",null,null);
但是等等……如果消息长度超过 160 个字符,这将不起作用……所以更好的方法:
String textSMS;
//short <160
// textSMS = "Hi Stackoverflow! its me Maher.";
//long >160
textSMS = "Hi Jerusalem, hi Cairo, Hi Prague, hi Baghdad, hi Riyadh, hi Jeddah, hi Dammam, hi Aleppo, hi Casablanca, hi Damascus, hi Alexandria, hi Algiers, hi Mosul, hi Basra, hi Arabia, hi Tripoli, hi Amman, hi Kuwait, hi Beirut, hi Abu Dhabi";
int simID = 0;//0:sim_1, 1:sim_2
ArrayList<String> messageList = SmsManager.getDefault().divideMessage(textSMS);
if (messageList.size() > 1) {
SimUtil.sendMultipartTextSMS(this, simID, "00972XXXXXXXXX", null, messageList, null, null);
} else {
SimUtil.sendSMS(this, simID, "00972XXXXXXXXX", null, textSMS, null, null);
}
这样您就可以安全地传递消息正文而不必担心长度。
------------更新 09.10.2016---------
要在 MultipartMessage 中使用 PendingIntent/DeliveryIntent.. 只需创建具有相同内容的 ArrayList 并传递它。下面是创建 PendingIntent 列表的实现:
final static String sSMSManagerIntentSENT = "package.DeliveryReport.SMS_SENT";
int numParts = parts.size();
ArrayList<PendingIntent> pendingIntents = new ArrayList<PendingIntent>();
for (int i = 0; i < numParts; i++) {
Intent pendingIntent = new Intent(sSMSManagerIntentSENT);
//optional if you want to keep info about what action has been done for feedback or analysis later when message is sent
pendingIntent.putExtra("package.DeliveryReport.phoneNumber", phoneNo); // receiver phoneNo
pendingIntent.putExtra("package.DeliveryReport.textSMS", msg);// msg body
pendingIntent.putExtra("SIM", simID); // which sim is sending this message
pendingIntents.add(PendingIntent.getBroadcast(getActivity(), 0, pendingIntent,PendingIntent.FLAG_ONE_SHOT));
}
对于交付,只需使用相同的方法。
------------------ 额外 ------------------
我已经看到 Android 22 支持 Android 5.1 的多 sim 卡,这里是如何使用它.. 不幸的是我没有该版本的设备进行测试,所以请反馈:
SmsManager.getSmsManagerForSubscriptionId(int subscriptionId).sendTextMessage(String destinationAddress, String scAddress, String text,PendingIntent sentIntent, PendingIntent deliveryIntent);
如何获取订阅 ID?查看属于 sim 卡的所有可用订阅 ID:
SubscriptionManager subscriptionManager = SubscriptionManager.from(getApplicationContext());
List<SubscriptionInfo> subscriptionInfoList = subscriptionManager.getActiveSubscriptionInfoList();
for (SubscriptionInfo subscriptionInfo : subscriptionInfoList) {
int subscriptionId = subscriptionInfo.getSubscriptionId();
Log.d("apipas","subscriptionId:"+subscriptionId);
}
** 请注意,此代码适用于 5.1。如果你尝试在旧版本上运行它,你会得到一个方法不存在的异常。
------------更新 19.8.2015------------
SIM 卡信息位于 DB:telephony.db(默认为:/data/data/com.android.providers.telephony/databases/telephony.db)中的 siminfo 表中。请参阅真实设备上 DB 中表 siminfo 的屏幕截图。
幸运的是,有一个内容提供者:
"content://telephony/siminfo/"
所以基本上只是从该表中查询数据。重要的是要提到插槽 0 代表 SIM1,1 代表 SIM2,插槽 -1 来自旧/移除/替换的 SIM。
这适用于联想 A319。我想这可能适用于其他设备。这是我使用的 util 方法:
public static List<SimInfo> getSIMInfo(Context context) {
List<SimInfo> simInfoList = new ArrayList<>();
Uri URI_TELEPHONY = Uri.parse("content://telephony/siminfo/");
Cursor c = context.getContentResolver().query(URI_TELEPHONY, null, null, null, null);
if (c.moveToFirst()) {
do {
int id = c.getInt(c.getColumnIndex("_id"));
int slot = c.getInt(c.getColumnIndex("slot"));
String display_name = c.getString(c.getColumnIndex("display_name"));
String icc_id = c.getString(c.getColumnIndex("icc_id"));
SimInfo simInfo = new SimInfo(id, display_name, icc_id, slot);
Log.d("apipas_sim_info", simInfo.toString());
simInfoList.add(simInfo);
} while (c.moveToNext());
}
c.close();
return simInfoList;
}
这里是实体类 SimInfo:
public class SimInfo {
private int id_;
private String display_name;
private String icc_id;
private int slot;
public SimInfo(int id_, String display_name, String icc_id, int slot) {
this.id_ = id_;
this.display_name = display_name;
this.icc_id = icc_id;
this.slot = slot;
}
public int getId_() {
return id_;
}
public String getDisplay_name() {
return display_name;
}
public String getIcc_id() {
return icc_id;
}
public int getSlot() {
return slot;
}
@Override
public String toString() {
return "SimInfo{" +
"id_=" + id_ +
", display_name='" + display_name + '\'' +
", icc_id='" + icc_id + '\'' +
", slot=" + slot +
'}';
}
}
祝你好运,'。
关于android - 如何在双卡手机中使用 SMSmanager 发送短信?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27351936/
由于某种原因,当 php mail() 发送到文本号码(通过电子邮件)时,即 123456789@vtext.com;发件人显示为 centos apache 服务器输出电子邮件 (apache@ho
我正在编写一个需要用户输入手机号码的新网站,我面临的问题是我需要确保用户实际上是(或在这种情况下,有权访问)的所有者手机号码。 我想出的解决方案是,在提交号码时,我向他们发送带有 token 的短信,
我目前正在尝试编写一个应用程序来拦截文本消息并根据该消息的内容使用react。我试图挂接到 CKSMSService 类中的 _receivedMessage:(struct __CKSMSRecor
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 4年前关闭。 Improve thi
Android/iPhone/Windows mobile/Symbian 是否有任何一种标准/广泛使用的 SMS 格式可以发送/接收 GPS 坐标(“地理标记 SMS”)? 例如Garmin 具有对
当用户的桌面应用程序上发生警报时,我想向用户的智能手机发送电子邮件或短信。 如何在不提供应用程序内登录凭据的情况下发送电子邮件? 使用 smtp,我需要提供我的凭据,但由于这是针对已发布的应用程序,这
有多久,没有发过短信了? 1、背景简介 在常规的分布式架构下,「消息中心」的服务里通常会集成「短信」的渠道,作为信息触达的重要手段,其他常用的手段还包括:「某微」、「某钉」、
我基本上是在尝试允许用户使用短信发布到他们的博客。我为每个用户存储了一个电话号码,并且由于 twilio 在他们提交给我的页面的发布请求中发送该信息,我可以进行反向查找以查看将其发布到哪个博客。现在出
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
我想从 jSMPP 接收 Unicode 短信。 如果数据编码是8,我想将其转换为Unicode符号。 为此,我使用 HexUtil.convertBytesToHexString 函数。 但它没有正
我正在尝试使用 USB 调制解调器发送短信(我有一个 D-Link USB 调制解调器)。我刚找到 SmsLib,但我不知道如何使用 USB 调制解调器。我找到的例子是串行调制解调器,我没有找到任何
我想为网站管理员做一个简单的通知/新闻系统。 将这些消息(最多 250 个字符)存储在数据库中的最佳解决方案是什么? VARCHAR(255) 还是 TEXT? 谢谢 最佳答案 如果它们要短,定义一个
传入的 Type-0 SMS(请注意:“Type-0”是 ment,而不是“Class-0”)如何被 BroadcastReceiver 捕获? 背景:出于配置目的,发送 Type-0 SMS(带有
我在我的 android 应用程序中尝试了这段代码来获取 SMS 消息,但它不起作用,该应用程序没有出现在消息列表中。我应该添加一些东西来让它工作吗?
我在这里的两篇文章中找到了这段代码: String url = "content://sms/"; Uri uri = Uri.parse(url); getContentResolver().reg
有几个示例说明如何在具有高优先级的广播接收器中读取 SMS,然后调用 abortBroadcast() 以阻止 Intent 到达其他应用程序的广播接收器。但是,它们似乎都没有考虑到 Intent 数
我的 sendSms 函数有 try 和 catch block 。 Try block 是默认设备短信应用程序。 catch block 是设置为当前短信应用程序的其他短信应用程序,例如谷歌短信应用
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 7年前关闭。 Improve this qu
客户询问我们是否可以向订阅者发送免费短信。他们想知道是否有办法不向收件人收取入站短信费用? 我们与几个短信服务合作,我不认为这是一个选项。我的假设是您需要与每个运营商合作并从他们的系统发送消息才能完成
我是一名优秀的程序员,十分优秀!