首先创建基于Android2.2 模拟器的Android工程
先完善string.xml 文件
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, SmsActivity!</string> <string name="app_name">短信发送器</string> <string name="mobile">请输入手机号</string> <string name="content">请输入短信内容</string> <string name="button">发送短信</string> <string name="success">发送成功</string> </resources>然后完善main.xml 界面UI文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/mobile" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/mobile" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/content" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/content" android:minLines="3" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button" android:text="@string/button" /> </LinearLayout>Java类
package com.android.sms; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.telephony.SmsManager; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class SmsActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btn=(Button)this.findViewById(R.id.button); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { EditText mobileText=(EditText)findViewById(R.id.mobile); EditText contentText=(EditText)findViewById(R.id.content); /** * 获取手机号 */ String mobile=mobileText.getText().toString(); /** * 获取短信内容 */ String content=contentText.getText().toString(); /** * 获取系统的短信管理器 */ SmsManager sm=SmsManager.getDefault(); /** * 如果短信超过70个字符,将短信拆分进行发送。 */ List<String> texts=sm.divideMessage(content); for(String text:texts){ sm.sendTextMessage(mobile, null, text, null, null); } /** * 添加一个发送结果提示 */ Toast.makeText(SmsActivity.this, R.string.success, Toast.LENGTH_LONG).show(); } }); } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.sms" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <uses-permission android:name="android.permission.SEND_SMS"/> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".SmsActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Hi,推荐文件给你 "sms.zip" 点击打开链接