参照网上的例子,做了一个手机发送短信的app。
有两种发送方法:第一种方法测试没有信息的发送记录,第二种调用的sms的Activity还要自行操作。
首先是xml布局文件代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/main_title" /><EditTextandroid:id="@+id/phone"android:layout_width="fill_parent"android:layout_height="wrap_content"android:hint="@string/main_phone"android:inputType="phone" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/main_text" /><EditTextandroid:id="@+id/text"android:layout_width="fill_parent"android:layout_height="wrap_content"android:inputType="textMultiLine" /><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:gravity="center_horizontal"android:orientation="vertical" ><Buttonandroid:id="@+id/send"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/main_send" /><Buttonandroid:id="@+id/send2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/main_send" /></LinearLayout></LinearLayout>
然后是strings.xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources><string name="app_name">SendText</string><string name="action_settings">Settings</string><string name="hello_world">Hello world!</string><string name="main_title">发送短信</string><string name="main_phone">请输入电话号码</string><string name="main_text">请输入内容</string><string name="main_send">发送</string><string name="main_send_finish">发送完成</string></resources>
MainActivity的代码
package cn.yzy.sendtext;import java.util.List;import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.telephony.gsm.SmsManager;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;public class MainActivity extends Activity {private EditText phone;private EditText text;private Button send;private Button send2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);phone = (EditText) findViewById(R.id.phone);text = (EditText) findViewById(R.id.text);send = (Button) findViewById(R.id.send);send2 = (Button) findViewById(R.id.send2);send.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubString phone_number = phone.getText().toString().trim();String sms_content = text.getText().toString().trim();if (phone_number.equals("")) {Toast.makeText(MainActivity.this, R.string.main_phone,Toast.LENGTH_LONG).show();} else {SmsManager smsManager = SmsManager.getDefault();if (sms_content.length() > 70) {List<String> contents = smsManager.divideMessage(sms_content);for (String sms : contents) {smsManager.sendTextMessage(phone_number, null,sms_content, null, null);}} else {smsManager.sendTextMessage(phone_number, null,sms_content, null, null);}Toast.makeText(MainActivity.this,R.string.main_send_finish, Toast.LENGTH_LONG).show();}}});send2.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubString phone_number = phone.getText().toString().trim();String sms_content = text.getText().toString().trim();Uri uri = Uri.parse("smsto://" + phone_number);Intent intent = new Intent(Intent.ACTION_SENDTO, uri);intent.putExtra("sms_body", "send detail");startActivity(intent);}});}@Overridepublic 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;}}
记得加发送短信的权限
<uses-permission android:name="android.permission.SEND_SMS" />