首先创建基于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() {@Overridepublic 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();}});            }}

在文件AndroidManifest.xml 添加发送短信的权限

<?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"点击打开链接



更多相关文章

  1. 一款常用的 Squid 日志分析工具
  2. GitHub 标星 8K+!一款开源替代 ls 的工具你值得拥有!
  3. RHEL 6 下 DHCP+TFTP+FTP+PXE+Kickstart 实现无人值守安装
  4. Linux 环境下实战 Rsync 备份工具及配置 rsync+inotify 实时同步
  5. Android(安卓)JNI使用和原理分析
  6. 向sd写文件时权限
  7. Eclipse+CDT+GDB调试android NDK程序
  8. android ctrl + 左键(鼠标左键)直接打开xml文件
  9. 解决WebView无法上传文件的缺陷

随机推荐

  1. Android(安卓)IPC入门——AIDL
  2. 『转』Android(安卓)多个UI库及组件资源
  3. android 通过Intent使用Bundle传递对象
  4. 【android开发记录片】2.基于Eclipse的JN
  5. Android模拟器环境中添加和删除apk应用程
  6. Rexsee API介绍:Android(安卓)WakeLock进
  7. Android(安卓)OpenGL ES(一)开发入门
  8. Android流量统计TrafficStats类的使用
  9. Android(安卓)NDK: Host 'awk' tool is o
  10. 在eclipse里配置Android(安卓)ndk环境 适