【问题标题】:create alert dialog on receiving sms在接收短信时创建警报对话框
【发布时间】:2012-09-02 10:43:33
【问题描述】:

我正在为 android 创建一个应用程序,它使用“way2sms.com”、“fullonsms.com”等网关发送短信,我已成功从这些网关发送消息,现在我想采取另一个步骤,即“SMS DELIVERY报告”。我对如何做到这一点有一个基本的想法。当我将短信发送到用户选择的号码之后,我将向用户发送一条短信,其中包含一些格式化的文本,例如“短信交付报告”。我想要做的是,当用户收到此类消息而不是我们收到正常短信时收到的正常通知时,应用程序会创建一个警报对话框,显示“消息已成功传递”。请指导我完成这项任务。以及如何确保这些消息不会进入用户的正常消息收件箱。

【问题讨论】:

    标签: android sms android-alertdialog


    【解决方案1】:

    此代码将涵盖您发送短信的所有可能情况。

    private 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);        
    }
    

    这是great tutorial

    【讨论】:

    • 我正在使用“way2sms.com”或“fullonsms.com”发送短信,我可以使用此代码吗?
    • 我不明白。您是说您正在使用他们的 API 发送短信?
    • 那我相信你可以!工作方式几乎相同。虽然我不是 100% 确定!最好你自己试一试,但我认为它应该可以工作!
    【解决方案2】:

    我使用this 教程,当时我正在创建类似的东西!

    只需跳转到处理收到的短信!

    【讨论】:

    • 但是如何阻止这些邮件进入用户的收件箱?
    • 只需添加 abortBroadcast();进入您的广播接收器(在检查它是否是正确的短信后)
    猜你喜欢
    • 1970-01-01
    • 2018-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-14
    • 1970-01-01
    • 2012-09-15
    相关资源
    最近更新 更多