标签:
1、视图
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context=".MainActivity" > 10 11 <TextView 12 android:id="@+id/tv_input_number" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:layout_alignParentLeft="true" 16 android:layout_alignParentTop="true" 17 android:text="@string/please_input_number" 18 android:textSize="20px" /> 19 20 <EditText 21 android:singleLine="true" 22 android:id="@+id/et_number" 23 android:layout_width="match_parent" 24 android:layout_height="wrap_content" 25 android:layout_alignLeft="@+id/tv_input_number" 26 android:layout_below="@+id/tv_input_number" 27 android:ems="10" 28 android:inputType="phone" > 29 30 </EditText> 31 32 <TextView 33 android:id="@+id/tv_input_content" 34 android:layout_width="wrap_content" 35 android:layout_height="wrap_content" 36 android:layout_alignLeft="@+id/et_number" 37 android:layout_below="@+id/et_number" 38 android:layout_marginTop="14dp" 39 android:textColor="#ff0000" 40 android:text="@string/please_input_conent" 41 android:textSize="20px" /> 42 43 <EditText 44 android:lines="5" 45 android:id="@+id/tv_content" 46 android:layout_width="match_parent" 47 android:layout_height="wrap_content" 48 android:layout_alignLeft="@+id/tv_input_content" 49 android:layout_below="@+id/tv_input_content" 50 android:layout_marginTop="16dp" 51 android:ems="10" 52 android:inputType="textMultiLine" /> 53 54 <Button 55 android:id="@+id/bt_send" 56 android:layout_width="wrap_content" 57 android:layout_height="wrap_content" 58 android:layout_alignLeft="@+id/tv_content" 59 android:layout_below="@+id/tv_content" 60 android:text="@string/send_message" /> 61 62 </RelativeLayout>
2、权限:
<uses-permission android:name="android.permission.SEND_SMS"/>
3、java代码
1 package com.example.sendmessage; 2 3 import java.util.ArrayList; 4 5 import android.os.Bundle; 6 import android.app.Activity; 7 import android.telephony.SmsManager; 8 import android.text.TextUtils; 9 import android.view.Menu; 10 import android.view.View; 11 import android.view.View.OnClickListener; 12 import android.widget.Button; 13 import android.widget.EditText; 14 import android.widget.Toast; 15 16 public class MainActivity extends Activity implements OnClickListener { 17 18 private EditText et_number; 19 private EditText tv_content; 20 @Override 21 protected void onCreate(Bundle savedInstanceState) { 22 super.onCreate(savedInstanceState); 23 setContentView(R.layout.activity_main); 24 et_number = (EditText)findViewById(R.id.et_number); 25 tv_content = (EditText)findViewById(R.id.tv_content); 26 27 Button bt_send = (Button)findViewById(R.id.bt_send); 28 bt_send.setOnClickListener(this); 29 } 30 31 @Override 32 public void onClick(View v) { 33 // TODO Auto-generated method stub 34 switch(v.getId()) 35 { 36 case R.id.bt_send: 37 String number = et_number.getText().toString().trim(); 38 String content = tv_content.getText().toString().trim(); 39 if(TextUtils.isEmpty(number) || TextUtils.isEmpty(content)){ 40 Toast.makeText(this, "电话号码或者内容不能为空", 0).show(); 41 return; 42 }else{ 43 SmsManager smsManager = SmsManager.getDefault(); 44 ArrayList<String> contents = smsManager.divideMessage(content); 45 for(String str : contents){ 46 smsManager.sendTextMessage(number, null, str, null, null); 47 } 48 } 49 break; 50 } 51 52 } 53 54 }
标签:
原文地址:http://www.cnblogs.com/zhongyinghe/p/5354438.html