2、AndroidManifest.xml配置文件
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.huangjie.sms" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:label="@string/app_name" android:name=".MainActivity" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.SEND_SMS"/> </manifest>
<?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/number" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/numberEdit" /> <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:minLines="3" android:id="@+id/contentEdit" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button" android:id="@+id/button" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, MainActivity!</string> <string name="app_name">Sms</string> <string name="number">请输入手机号码</string> <string name="content">请输入短信内容</string> <string name="button">发送</string> <string name="sucess">发送成功</string> </resources>
/* AUTO-GENERATED FILE. DO NOT MODIFY. * * This class was automatically generated by the * aapt tool from the resource data it found. It * should not be modified by hand. */ package cn.huangjie.sms; public final class R { public static final class attr { } public static final class drawable { public static final int ic_launcher=0x7f020000; } public static final class id { public static final int button=0x7f050002; public static final int contentEdit=0x7f050001; public static final int numberEdit=0x7f050000; } public static final class layout { public static final int main=0x7f030000; } public static final class string { public static final int app_name=0x7f040001; public static final int button=0x7f040004; public static final int content=0x7f040003; public static final int hello=0x7f040000; public static final int number=0x7f040002; public static final int sucess=0x7f040005; } }
package cn.huangjie.sms; import java.util.ArrayList; 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 MainActivity extends Activity { private EditText numberText; private EditText contentText; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); numberText = (EditText)findViewById(R.id.numberEdit); contentText = (EditText)findViewById(R.id.contentEdit); Button button = (Button)findViewById(R.id.button); button.setOnClickListener(new SmsOnClickListener()); } private final class SmsOnClickListener implements View.OnClickListener{ public void onClick(View v) { String number = numberText.getText().toString(); String content = contentText.getText().toString(); //取得默认的短信管理器对象 SmsManager manager = SmsManager.getDefault(); //第一个是短信要发往的号码 //第二个是短信中心的号码,输入null代表使用系统默认的短信中心号码 //第三个一条短信通常只允许发送七十个汉字,一旦超过限制,要拆分成几条发送出去 //第四个用于得到发送的状态 //第五个用于对方是否收到这条短信的状态 //四和五这两个参数都是需要移动网络返回电信号来知道的,所以这个方法的内部采用异步的方式来处理 //这个行为,一旦该方法api的内部根据返回的电信号对外发送出一个广播,然后把结果存放在广播里面 //使用广播接受者就可以接收到这个广播,得到发送的状态和对方是否收到的状态,如不要返回则传递null ArrayList<String> texts = manager.divideMessage(content); for(String text : texts){ manager.sendTextMessage(number, null, text, null, null); } //所谓的上下文就是与应用有关的,这些信息在整个应用的范围内都可以被访问到的.例如应用的包名 //MainActivity也可以当作一个上下文的对象,因为它的顶级父类是一个上下文对象Context //也可以使用getApplicationContext()或者使用MainActivity.this Toast.makeText(getApplicationContext(), R.string.sucess, Toast.LENGTH_LONG); } } }