【问题标题】:Android SMS Message delivery report intentAndroid SMS 消息传递报告意图
【发布时间】:2010-10-06 17:40:21
【问题描述】:

发送短信时,Android 不会触发传递意图。我正在 HTC EVO 4G 上的 Android 2.2 上对此进行测试。

这是当前代码。我看到“收到短信发送意图”。在日志中,但不是“收到 SMS 传递的意图。”。

// Constants
String SENT_ACTION = "SMS_SENT_ACTION";
String DELIVERED_ACTION = "SMS_DELIVERED_ACTION";
String CELL_NUMBER = "0000000000";
String MESSAGE = "Hello World!";

// SMS sent pending intent
PendingIntent sentIntent = PendingIntent.getBroadcast(this, 0,
        new Intent(SENT_ACTION), 0);

// SMS delivered pending intent
PendingIntent deliveredIntent = PendingIntent.getBroadcast(this, 0,
        new Intent(DELIVERED_ACTION), 0);

// SMS sent receiver
registerReceiver(new BroadcastReceiver(){
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "SMS sent intent received.");
    }
}, new IntentFilter(SENT_ACTION));

// SMS delivered receiver
registerReceiver(new BroadcastReceiver(){
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "SMS delivered intent received.");
    }
}, new IntentFilter(DELIVERED_ACTION));

// Send the SMS message
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(CELL_NUMBER, null, MESSAGE, sentIntent, deliveredIntent);

【问题讨论】:

  • 从目前部署在 Thunderbolt 中的操作系统开始,HTC 完全覆盖 SMS 发送和传递意图的所有结果。如果消息成功,则可以回复结果代码,但如果消息失败,HTC 会自动覆盖所有结果代码,您的代码不会触发,并自动重新发送消息。基本上,如果您尝试对 SMS 应用程序的发送和接收通知进行编码,那么您在 HTC 设备上浪费了您的时间!我将尝试向 HTC 发送电子邮件,如果我收到回复,我会感到震惊。
  • 有任何回应或新消息吗?

标签: android android-intent sms


【解决方案1】:

在你想发送短信的地方调用这个方法

private String SimState = "";
private String address = ""; // Recipient Phone Number
private String message = ""; // Message Body

private void sendSms() {
    if (isSimExists()) {
        try {
            String SENT = "SMS_SENT";

            PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);

            registerReceiver(new BroadcastReceiver() {
                @Override
                public void onReceive(Context arg0, Intent arg1) {
                    int resultCode = getResultCode();
                    switch (resultCode) {
                        case Activity.RESULT_OK:
                            Toast.makeText(getBaseContext(), "SMS sent", Toast.LENGTH_LONG).show();
                            break;
                        case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                            Toast.makeText(getBaseContext(), "Generic failure", Toast.LENGTH_LONG).show();
                            break;
                        case SmsManager.RESULT_ERROR_NO_SERVICE:
                            Toast.makeText(getBaseContext(), "No service", Toast.LENGTH_LONG).show();
                            break;
                        case SmsManager.RESULT_ERROR_NULL_PDU:
                            Toast.makeText(getBaseContext(), "Null PDU", Toast.LENGTH_LONG).show();
                            break;
                        case SmsManager.RESULT_ERROR_RADIO_OFF:
                            Toast.makeText(getBaseContext(), "Radio off", Toast.LENGTH_LONG).show();
                            break;
                    }
                }
            }, new IntentFilter(SENT));

            SmsManager smsMgr = SmsManager.getDefault();
            smsMgr.sendTextMessage(address, null, message, sentPI, null);
        } catch (Exception e) {
            Toast.makeText(this, e.getMessage() + "!\n" + "Failed to send SMS", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
    } else {
        Toast.makeText(this, SimState + " " + "Cannot send SMS", Toast.LENGTH_LONG).show();
    }
}


// For receiving sms

class SMSReceiver extends BroadcastReceiver {
    private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent != null && intent.getAction() != null && ACTION.compareToIgnoreCase(intent.getAction()) == 0) {
            // Sms Received Your code here
        }
    }
}

注意:您必须指定 android.permission.SEND_SMS 和 清单文件中的 android.permission.RECEIVE_SMS 权限和 也是接收者

    <receiver android:name=".SMSReceiver"  android:enabled="true">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tekeli.order"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="11" />
    <uses-permission android:name="android.permission.SEND_SMS" ></uses-permission>
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".ActivityOrderActivity"
            android:label="@string/app_name">
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".B" ></activity>
         <activity android:name=".C"></activity>
    </application>

</manifest>

【讨论】:

  • 什么是isSimExists()? , 也为 isSimExists() 方法添加代码 sn-p
  • @kirtiavaiya 请参考 androidsnippets.wordpress.com/2012/08/07/… 的 isSimExists 方法
  • 这不是他要问的问题。他在询问您未提供给 sendTextMessage() 函数的已交付 PendingIntent。
  • @ocross 我已经提供了,PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0); 再次验证我的答案
  • smsMgr.sendTextMessage(address, null, message, sentPI, null);这就是我所看到的。该函数的最后一个参数是他所说的交付意图。您提供了发送的意图。无论哪种方式,我都得出结论,交付的意图不会返回太多相关数据,因为意图几乎总是 Result.OK 甚至发送到无效数字。 developer.android.com/reference/android/telephony/gsm/…, java.lang.String, java.lang.String, android.app.PendingIntent, android.app.PendingIntent)
【解决方案2】:

这是一个很晚的答案。但是,它可能会对某人有所帮助。

有问题的代码可以正常工作,但是,唯一需要更改的是更改交付请求代码。两者不能是相同的请求代码。

在这里...并在真实设备上运行以查看交付报告。

EditText edNumber = findViewById(R.id.edNumber);
EditText edMessage = findViewById(R.id.edMessage);

String number = edNumber.getText().toString().trim();
String message = edMessage.getText().toString().trim();

// set pendingIntent for sent & delivered

        PendingIntent sentIntent = PendingIntent.getBroadcast(this, 100, new 
Intent(SENT_ACTION), 0);

        PendingIntent deliveryIntent = PendingIntent.getBroadcast(this, 200, new 
Intent(DELIVERY_ACTION), 0);

        registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                Log.d("SMS ", "sent");
            }
        }, new IntentFilter(SENT_ACTION));

        registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                Log.d("SMS ", "delivered");
            }
        }, new IntentFilter(DELIVERY_ACTION));

        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(number, null, message, sentIntent, 
deliveryIntent);

【讨论】:

    猜你喜欢
    • 2021-07-20
    • 2019-05-19
    • 1970-01-01
    • 2011-10-28
    • 1970-01-01
    • 2011-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多