在Android应用程序中发送SMS

时间:2011-08-01 11:18:40

标签: android

我发了一个发送申请。如何在没有实际设备的情况下检查邮件发送?

sendSms.setOnClickListener(new View.OnClickListener()
{
    public void onClick(View v)
    {
        //Toast.makeText(getApplicationContext(),template+"and"+phoneNumber,Toast.LENGTH_LONG).show();
        if (phoneNumber.length()>0 && template.length()>0)
        {
            sendSMS(phoneNumber, template);
        }
        else
            Toast.makeText(getApplicationContext(), "please select name & template",Toast.LENGTH_SHORT).show();
    }

    private void sendSMS(String phoneNumber, String template)
    {
        PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0,new Intent(getApplicationContext(),Test2Activity.class), 0);
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, template, pi, null);
    }
});

2 个答案:

答案 0 :(得分:3)

  1. 打开两个模拟器。
  2. 在一个模拟器中运行您的程序。
  3. 在“sms.sendTextMessage(phoneNumber,null,template,pi,null)”行中;“用模拟器号代替phoneNumber。例如,5554/5556等
  4. 成功运行代码后,其他模拟器将收到短信。

答案 1 :(得分:0)

您可以这样检查: -

registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context context, Intent intent) {
                 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));


registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            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));

Umesh的回答是绝对正确的,但如果你想以实用的方式检查它,那么你必须在你的Activity中添加这个代码。