如何在Android中发送消息?

时间:2012-08-29 10:15:30

标签: android button message send

目前我正在使用Android中的消息撰写屏幕,使用Intent显示消息撰写屏幕,然后我输入了电话号码和消息。

我为发送按钮设置了sendSMS方法但是,当我按下发送按钮时,它没有调用sendSMS方法。

如何在消息撰写屏幕中设置发送按钮的方法?请帮我。 是否可以设置发送按钮的方法?

先谢谢

供您参考的源代码:

Texts.Java类

public class Texts extends Activity
{
     public void onCreate(Bundle savedInstanceState)
     {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.message_tab_screen);

            Button Compose =(Button) findViewById(R.id.button1);
            Compose.setOnClickListener(new Button.OnClickListener() 
            {
                public void onClick(View v)
                {
                    Intent intent = new Intent("android.intent.action.VIEW");
                    intent.putExtra("sms_body", "");
                    Uri data = Uri.parse("sms:");
                    intent.setData(data);
                    startActivity(intent);
                }
            });
     }

     //---sends an SMS message method
        private void sendSMS(String phoneNumber, String message)
        {        
                    System.out.println("SEND MESSAGE");
        }    


}

message_tab_screen.xml 文件

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:text="Message Compose " />       
    </RelativeLayout>

enter image description here

4 个答案:

答案 0 :(得分:1)

使用内置短信应用发送短信:

Intent i = new Intent(android.content.Intent.ACTION_VIEW);

i.putExtra("address", "09090909; 092322424; 123456778");

i.putExtra("sms_body", "SMS Content");

i.setType("vnd.android-dir/mms-sms");

startActivity(i);

在没有用户界面的情况下发送短信:

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

答案 1 :(得分:0)

Creating a SMS Application in Android?

http://mobiforge.com/developing/story/sms-messaging-android

尝试此示例以创建自定义SMS应用程序并为发送按钮设置操作。

答案 2 :(得分:0)

public class MainActivity extends Activity {
   Button btnSendSMS;
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
        btnSendSMS.setOnClickListener(new View.OnClickListener()
        {
             public void onClick(View v)
            {
                 sendSMS(“5556”, “Hello my friends!”);
            }
        });
   }
   //---sends an SMS message to another device---
   private void sendSMS(String phoneNumber, String message)
   {
             SmsManager sms = SmsManager.getDefault();
             sms.sendTextMessage(phoneNumber, null, message, null, null);
   }
}

使用此代码..........

答案 3 :(得分:-2)

实际上试试这个

btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
btnSendSMS.setOnClickListener(new View.OnClickListener()
{
       public void onClick(View v)
       {
             sendSMS(“5556”, “Hello my friends!”);
        }
});
相关问题