`
gryphone
  • 浏览: 426041 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

短信发送状态侦听

阅读更多

短信发送后回调提示

 

 

信息发送后 可能有如下需求: 通知用户信息已发送 那如何实现?

 

 

SmsManager.sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)

 

1. 传入参数:sentIntent

2. 注册BroadcastReceiver 用于接受sentIntent

 

 

 

[代码]

 

 

public void sendSMS(String phoneNumber, String message) {        
        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:
                        Toast.makeText(getBaseContext(), "SMS sent", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(getBaseContext(), "Generic failure", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(getBaseContext(), "No service", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(getBaseContext(), "Null PDU", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(getBaseContext(), "Radio off", 
                                Toast.LENGTH_SHORT).show();
                        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:
                        Toast.makeText(getBaseContext(), "SMS delivered", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case Activity.RESULT_CANCELED:
                        Toast.makeText(getBaseContext(), "SMS not delivered", 
                                Toast.LENGTH_SHORT).show();
                        break;                        
                }
            }
        }, new IntentFilter(DELIVERED));        
 
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);    
        
    }

 

 

 

代码也比较简单 故此不多说 大家自己看看就能明白~

 

 

最后 别忘了回帖哦 相互交流

分享到:
评论
5 楼 xiangdream 2011-09-07  
good, thanks for your sharing
4 楼 lmhdlcode 2010-08-05  
如何在

case Activity.RESULT_OK: 
                        Toast.makeText(getBaseContext(), "SMS delivered",  
                                Toast.LENGTH_SHORT).show(); 
                        break; 

获得电话号码

判断一下哪个电话短信发送成功了。
3 楼 lp518 2010-07-10  
gryphone 写道
lp518 写道
请问一下,这个能侦听到模拟器中自带的Message中发送短信的事件吗?发件箱中存没存收件人的号码呀?谢谢~

不行 因为我的这个方法是通过Pe那就是:dingIntent来做到的 不过我的另一篇文章可以 那就是:SMS管理 那个是通过sms数据库监听的

谢谢~
2 楼 gryphone 2010-06-15  
lp518 写道
请问一下,这个能侦听到模拟器中自带的Message中发送短信的事件吗?发件箱中存没存收件人的号码呀?谢谢~

不行 因为我的这个方法是通过Pe那就是:dingIntent来做到的 不过我的另一篇文章可以 那就是:SMS管理 那个是通过sms数据库监听的
1 楼 lp518 2010-06-14  
请问一下,这个能侦听到模拟器中自带的Message中发送短信的事件吗?发件箱中存没存收件人的号码呀?谢谢~

相关推荐

Global site tag (gtag.js) - Google Analytics