如何使用Android从一部手机向另一部手机发送短信?

时间:2015-03-07 10:20:16

标签: android sms

我想使用android将短信发送到另一部手机。以下是我的代码。但它没有工作

package com.example.smssend;

import android.os.Bundle;
import android.app.Activity;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

    Button sendBtn;
    EditText txtphoneNo;
    EditText txtMessage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      sendBtn = (Button) findViewById(R.id.button1);
      txtphoneNo = (EditText) findViewById(R.id.phno);
      txtMessage = (EditText) findViewById(R.id.msg);

      sendBtn.setOnClickListener(new View.OnClickListener()
      {
         public void onClick(View view)
         {
            sendSMSMessage();
         }
      });
      }
     protected void sendSMSMessage()
     {
           Log.i("Send SMS", "");


      String phoneNo = txtphoneNo.getText().toString();
      String message = txtMessage.getText().toString();
      try
      {

         SmsManager smsManager = SmsManager.getDefault();
         smsManager.sendTextMessage(phoneNo, null, message, null, null);
         Toast.makeText(getApplicationContext(), "SMS sent.",Toast.LENGTH_LONG).show();
      } 
      catch (Exception e) 
      {
         Toast.makeText(getApplicationContext(), "SMS failed, please try again.",Toast.LENGTH_LONG).show();
         e.printStackTrace();
      }
       } 

     @Override
     public boolean onCreateOptionsMenu(Menu menu)
     {
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
     }
}

请帮忙.. 如果这是错误的。告诉我任何其他建议发送短信到另一个手机。 它显示"短信已发送"当我在我的手机上运行此代码时。但接收方没有收到短信。 谢谢你提前

0 个答案:

没有答案