【问题标题】:Send message using content of a editText使用 editText 的内容发送消息
【发布时间】:2014-03-17 11:30:52
【问题描述】:

我正在尝试使用 editText 的内容作为 SMS 内容从我的应用程序发送消息。到目前为止,我尝试过这种方式:

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

但是当活动开始时,消息中没有内容..不可能做这样的事情吗?

【问题讨论】:

  • 您发布的代码是正确的。

标签: java android android-edittext sendmessage


【解决方案1】:

尝试发送这样的消息

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

【讨论】:

    【解决方案2】:

    我尝试使用与您相同的代码,但在方法调用中。这是因为如果您的设备上安装了超过 1 个消息传递应用程序,您将必须手动选择要用于发送消息的应用程序。 就我而言,我有whatsapp,消息传递等。因此,将您的代码放在onCreate()中,它只会失败,因为用户没有选择任何选项......但是当我在方法主体中有此代码并触发时使用按钮或任何其他类似的按钮,它都可以正常工作(唯一的事情是您必须选择应用程序 - 您可以将任何应用程序设置为默认应用程序,以便下次您不必手动执行此操作)。

    我的 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;
        }
    
    }
    

    我的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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-11
      • 2013-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-25
      • 1970-01-01
      相关资源
      最近更新 更多