2012-08-29 132 views
0

目前我正在Android中的消息撰寫屏幕上工作,使用Intent顯示消息撰寫屏幕,然後輸入電話號碼和消息。如何在Android中發送消息?

我已經設置了一個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

+0

其實你調用系統意圖 Intent intent = new Intent(「android.intent.action.VIEW」); 因此,您無法爲發送按鈕設置任何控件。你必須設計自定義頁面和按鈕來實現你的想法。 –

+0

感謝您的回覆,是的,我用意圖創建消息撰寫屏幕,是否可以設置發送按鈕的方法? – SampathKumar

回答

-2

其實試試這個

btnSendSMS = (Button) findViewById(R.id.btnSendSMS); 
btnSendSMS.setOnClickListener(new View.OnClickListener() 
{ 
     public void onClick(View v) 
     { 
      sendSMS(「5556」, 「Hello my friends!」); 
     } 
}); 
+0

感謝您的回覆,我沒有在佈局屏幕中使用任何按鈕。 – SampathKumar

+0

SmsManager sms = SmsManager.getDefault();使用這個短信管理器 – Satya

+1

編輯你的答案,不要添加新的答案,除非他們實際上是不同的問題答案。 – Will

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

使用此代碼..........

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

發送短信沒有UI:

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