确保SMS是从Android Messaging应用程序发送的

时间:2013-05-28 14:05:01

标签: android sms messaging

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

我正在建立一个游戏的短信邀请系统,为每个发送的邀请提供奖励。 我想要的是为用户点击“取消”时创建两种不同的行为,以及当用户点击“发送”时(EG,如果用户点击取消则“未收到奖励”,如果用户点击发送则收到“奖励”)。< / p>

有什么想法吗?

谢谢, 伊塔马尔

1 个答案:

答案 0 :(得分: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); 
    }
}