当前位置: 动力学知识库 > 问答 > 编程问答 >

button - How to send a message in Android?

问题描述:

Currently I am working in message compose screen in Android, Using Intent to show message compose screen, then I have enter the phone number and messages.

I have set a sendSMS method for send button but, when I press send button it didn't call to sendSMS method.

How to set a method for send button in Message compose screen? please help me.

Is it possible to set method for send button?

Thanks in Advance

Source code for your reference:

Texts.Java class

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 file

 <?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>

网友答案:

Creating a SMS Application in Android?

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

Try this example to create custom SMS application and set your action for the send button.

网友答案:

Send SMS with Build-in SMS App:

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

Send SMS without UI:

 SmsManager sms = SmsManager.getDefault();
 sms.sendTextMessage(phoneNumber, null, message, null, null);
网友答案:
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);
   }
}

use this code..........

网友答案:

Actually try this

btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
btnSendSMS.setOnClickListener(new View.OnClickListener()
{
       public void onClick(View v)
       {
             sendSMS(“5556”, “Hello my friends!”);
        }
});
分享给朋友:
您可能感兴趣的文章:
随机阅读: