Java 使用editText的内容发送消息

Java 使用editText的内容发送消息,java,android,android-edittext,sendmessage,Java,Android,Android Edittext,Sendmessage,我正在尝试从我的应用程序发送一条消息,将其作为SMS内容使用editText的内容。到目前为止,我一直在这样做: Uri uri = Uri.parse("smsto:0800000123"); Intent it = new Intent(Intent.ACTION_SENDTO, uri); String content = edit.getText().toString(); it.putExtra("sms_body", content); startActivity(

我正在尝试从我的应用程序发送一条消息,将其作为SMS内容使用editText的内容。到目前为止,我一直在这样做:

Uri uri = Uri.parse("smsto:0800000123");   
Intent it = new Intent(Intent.ACTION_SENDTO, uri);   
String content = edit.getText().toString();
it.putExtra("sms_body", content);   
startActivity(it);

但当活动开始时,消息中没有任何内容。。不能这样做吗?

尝试发送这样的消息

String phoneNo = "080000123";
String sms = textsms.getText().toString();
            try 
            {
                android.telephony.SmsManager smsmanager = android.telephony.SmsManager.getDefault();
                smsmanager.sendTextMessage(phoneNo, null, sms, null, null);
                Toast.makeText(getApplicationContext(), "SMS Sent!",Toast.LENGTH_LONG).show();
            } 
            catch (Exception e) 
            {
                    Toast.makeText(getApplicationContext(),"SMS faild, please try again later!",Toast.LENGTH_LONG).show();
                    e.printStackTrace();
            }

我尝试使用与您相同的代码,但在方法调用中。这是因为如果您的设备上安装了多个消息应用程序,则必须手动选择要发送消息的应用程序。 在我的例子中,我有whatsapp、messaging等。因此,将代码放在onCreate()中只会失败,因为用户没有选择任何选项……但是当我在方法体中有此代码,并且我用按钮或任何其他类似的方式触发时,它工作正常(唯一的问题是你必须选择你可以将任何一个应用设为默认的应用,这样下次你就不必手动操作了)

My MainActivity.java

   public class MainActivity extends Activity
{

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

    public void send(View v)
    {
        Uri uri = Uri.parse("smsto:9999999999");
        Intent it = new Intent(Intent.ACTION_SENDTO, uri);
        EditText edit = (EditText) findViewById(R.id.editText1);
        String content = edit.getText().toString();
        it.putExtra("sms_body", content);
        startActivity(it);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
My activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:onClick="send" />

</LinearLayout>

您发布的代码是正确的。