0
有很多類似於此的線程,但沒有一個能夠很好地回答我的問題。確保SMS來自Android Messaging應用程序
我正在爲一款遊戲構建一個短信邀請系統,該系統會爲每次發送的邀請給予獎勵。 我想要創建兩個不同的行爲,當用戶點擊「取消」和用戶點擊「發送」時(E.G,「未收到獎勵」,如果用戶點擊取消,「收到獎勵」,如果用戶點擊發送)。
任何想法?
感謝, 伊塔馬爾
有很多類似於此的線程,但沒有一個能夠很好地回答我的問題。確保SMS來自Android Messaging應用程序
我正在爲一款遊戲構建一個短信邀請系統,該系統會爲每次發送的邀請給予獎勵。 我想要創建兩個不同的行爲,當用戶點擊「取消」和用戶點擊「發送」時(E.G,「未收到獎勵」,如果用戶點擊取消,「收到獎勵」,如果用戶點擊發送)。
任何想法?
感謝, 伊塔馬爾
這裏是一個代碼示例。 您可以監視不同情況下的結果。
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);
}
}