gpt4 book ai didi

android - 通过 Intent 发送短信并知道短信是否已发送

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:31:34 26 4
gpt4 key购买 nike

我尝试使用此代码通过 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com