0

有很多类似的线程,但没有一个能完全回答我的问题。

我正在为一个游戏构建一个短信邀请系统,它会为每个发送的邀请提供奖金。我想要的是为用户点击“取消”和用户点击“发送”时创建两种不同的行为(例如,如果用户点击取消,则“未收到奖金”,如果用户点击发送,则“收到奖金”)。

有任何想法吗?

谢谢, 伊塔马尔

4

1 回答 1

1

这是一个代码示例。您可以监控不同情况下的结果。

private void sendSMS(final HashSet<String> phoneNumbers, String smsBody)
{        
    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";

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

    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);

    //---when the SMS has been sent---
    registerReceiver(new BroadcastReceiver(){
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode())
            {
                case Activity.RESULT_OK:
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    break;
            }
        }
    }, new IntentFilter(SENT));

    //---when the SMS has been delivered---
    registerReceiver(new BroadcastReceiver(){
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode())
            {
                case Activity.RESULT_OK:
                    break;
                case Activity.RESULT_CANCELED:
                    break;                        
            }
        }
    }, new IntentFilter(DELIVERED));        

    SmsManager sms = SmsManager.getDefault();
    for(String phoneNumber : phoneNumbers){
        sms.sendTextMessage(phoneNumber, null, smsBody, sentPI, deliveredPI); 
        // saves the sms in the sms sent folder 
        ContentValues values = new ContentValues();
        values.put("address", phoneNumber);
        values.put("body", smsBody);
        // Note: This uses an Android internal API to save to Sent-folder
        mContext.getContentResolver().insert(Uri.parse("content://sms/sent"), values); 
    }
}
于 2013-06-09T09:09:45.117 回答