- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我尝试使用此代码通过 Intent 发送短信:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("smsto:" + phoneNumber));
intent.putExtra("address", phoneNumber);
intent.putExtra("sms_body", messageBody);
intent.putExtra("exit_on_sent", true);
startActivityForResult(intent, CODE);
然后,我想知道短信是否已发送,我使用此代码:
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
switch (requestCode) {
case CODE:
if (resultCode == Activity.RESULT_OK)
{
//Then do...
}
elseif(resultCode == Activity.RESULT_CANCELED)
{
// Do...
}
break;
}
}
问题是结果始终为 0 (Activity.RESULT_CANCELED),即使已发送 SMS。我如何知道短信是否已发送?我想使用手机的短信默认应用程序,而不是创建一个发送短信的接口(interface)。
最佳答案
在下面的示例中,我们使用 ContentObserver
来监控对 SMS Provider 的更新。这个 Observer 在 SMS Intent 被触发之前创建和启动,并根据目标地址检查 Provider 的变化。创建观察者的 Activity 必须实现 SmsSendObserver.SmsSendListener
接口(interface)以接收回调。
Observer 的构造函数包含一个timeout
参数(以毫秒为单位),以便在合理的时间后未发送消息时允许 Observer 正确注销。如果需要,可以将其设置为 NO_TIMEOUT
。但是,正如所写的那样,该类仅供“一次性”使用,它将自行注销并在回调时使成员无效。如果没有回调发生,可以使用stop()
方法进行清理。在任何一种情况下,该实例都不再可用,并且对它的任何引用都应设置为 null。
示例 Activity :
public class MainActivity extends Activity
implements SmsSendObserver.SmsSendListener {
...
private void sendMessage(String phoneNumber, String messageBody) {
// This example has a timeout set to 15 seconds
new SmsSendObserver(this, phoneNumber, 15000).start();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("smsto:" + phoneNumber));
intent.putExtra("address", phoneNumber);
intent.putExtra("sms_body", messageBody);
intent.putExtra("exit_on_sent", true);
startActivity(intent);
}
public void onSmsSendEvent(boolean sent) {
Toast.makeText(this, sent ? "Message was sent" : "Timed out",
Toast.LENGTH_SHORT).show();
}
}
SmsSendObserver
类:
public class SmsSendObserver extends ContentObserver {
public static final int NO_TIMEOUT = -1;
private static final Handler handler = new Handler();
private static final Uri uri = Uri.parse("content://sms/");
private static final String COLUMN_ADDRESS = "address";
private static final String COLUMN_TYPE = "type";
private static final String[] PROJECTION = { COLUMN_ADDRESS, COLUMN_TYPE };
private static final int MESSAGE_TYPE_SENT = 2;
private Context context = null;
private ContentResolver resolver = null;
private String phoneNumber = null;
private long timeout = NO_TIMEOUT;
private boolean wasSent = false;
private boolean timedOut = false;
public SmsSendObserver(Context context, String phoneNumber, long timeout) {
super(handler);
if (context instanceof SmsSendListener) {
this.context = context;
this.resolver = context.getContentResolver();
this.phoneNumber = phoneNumber;
this.timeout = timeout;
}
else {
throw new IllegalArgumentException(
"Context must implement SmsSendListener interface");
}
}
private Runnable runOut = new Runnable() {
@Override
public void run() {
if (!wasSent) {
timedOut = true;
callBack();
}
}
};
public void start() {
if (resolver != null) {
resolver.registerContentObserver(uri, true, this);
if (timeout > NO_TIMEOUT) {
handler.postDelayed(runOut, timeout);
}
}
else {
throw new IllegalStateException(
"Current SmsSendObserver instance is invalid");
}
}
public void stop() {
if (resolver != null) {
resolver.unregisterContentObserver(this);
resolver = null;
context = null;
}
}
private void callBack() {
((SmsSendListener) context).onSmsSendEvent(wasSent);
stop();
}
@Override
public void onChange(boolean selfChange) {
if (wasSent || timedOut)
return;
Cursor cursor = null;
try {
cursor = resolver.query(uri, PROJECTION, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
final String address =
cursor.getString(cursor.getColumnIndex(COLUMN_ADDRESS));
final int type =
cursor.getInt(cursor.getColumnIndex(COLUMN_TYPE));
if (PhoneNumberUtils.compare(address, phoneNumber) &&
type == MESSAGE_TYPE_SENT) {
wasSent = true;
callBack();
}
}
}
finally {
if (cursor != null) {
cursor.close();
}
}
}
public interface SmsSendListener {
// Passes true if the message was sent
// Passes false if timed out
public void onSmsSendEvent(boolean sent);
}
}
关于android - 通过 Intent 发送短信并知道短信是否已发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26090381/
由于某种原因,当 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
客户询问我们是否可以向订阅者发送免费短信。他们想知道是否有办法不向收件人收取入站短信费用? 我们与几个短信服务合作,我不认为这是一个选项。我的假设是您需要与每个运营商合作并从他们的系统发送消息才能完成
我是一名优秀的程序员,十分优秀!