每次发送短信时都会显示Toast消息

时间:2014-02-18 05:42:29

标签: android smsmanager

这是我在 ActivityOne

中发送短信的代码
        private void sendSMS(final String phoneNumber, final String message,final String userMsg)
        {
            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 toast = Toast.makeText(getApplicationContext(), userMsg, Toast.LENGTH_SHORT);
                        toast.show();

                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(getApplicationContext(), "SMS not sent, Generic failure",
                        Toast.LENGTH_SHORT).show();
                    break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(getApplicationContext(), "SMS not sent, No service",
                        Toast.LENGTH_SHORT).show();
                    break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(getApplicationContext(), "SMS not sent,, Null PDU",
                        Toast.LENGTH_SHORT).show();
                    break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(getApplicationContext(), "SMS not sent, Radio off",
                        Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        }, new IntentFilter(SENT));

            SmsManager sms = SmsManager.getDefault();
            sms.sendTextMessage(phoneNumber, null, message, sentPI, null);

        }

我在Activity中称之为:

sendSMS(mobileNumber, "My Message", "SMS sent successfully");

我在下一个活动 ActivityTwo 中使用相同的sendSMS方法,但是当我从 ActivityTwo 发送短信时,它会显示我每次最后一次活动的干杯 ActivityOne

感谢。

3 个答案:

答案 0 :(得分:1)

您可以通过添加一个参数this.context来设置当前上下文,并像下面这样调用toast:

 private void senSMS(final String phoneNumber, final String message,final String userMsg,Context context)
{
//do something
Toast.makeText(context, "SMS sent",Toast.LENGTH_SHORT).show();
}

答案 1 :(得分:0)

如果您有Toast参考,可以取消Toast,但由于它是由单独的活动创建的,您无法访问它。

创建它:

  

Toast toast = Toast.makeText(context,text,duration);

现在您可以使用toast.cancel()取消祝酒词,请参阅Toast class

的文档

答案 2 :(得分:0)

可能存在注册和取消注册广播接收器的问题。

  

如果在Activity.onResume()实现中注册接收者,   你应该在Activity.onPause()中取消注册它。 (你不会收到的   意图暂停时,这将减少不必要的系统   高架)。不要在Activity.onSaveInstanceState()中取消注册,   因为如果用户在历史记录中移回,则不会调用此方法   叠加。

https://developer.android.com/reference/android/content/BroadcastReceiver.html

相关问题