上次成功实现了“拨打电话”后,心里兴奋极了,于是一口气研究了手机发送短信的原理,其实也很简单。

新建SMS,工程如下所示:



●修改res/values目录下的string.xml文件,如下所示:


[html] view plain copy print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <stringname="hello">HelloWorld,SMSActivity!</string>
  4. <stringname="app_name">发送短信</string>
  5. <stringname="mobile">请输入手机号</string>
  6. <stringname="content">请输入短信内容</string>
  7. <stringname="button">发送短信</string>
  8. <stringname="sendSucc">发送成功!</string>
  9. </resources>


●修改res/layout目录下的main/xml文件


[html] view plain copy print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical">
  6. <TextView
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:text="@string/mobile"/>
  10. <EditText
  11. android:id="@+id/mobile"
  12. android:layout_width="fill_parent"
  13. android:layout_height="wrap_content">
  14. <requestFocus/>
  15. </EditText>
  16. <TextView
  17. android:id="@+id/textView1"
  18. android:layout_width="fill_parent"
  19. android:layout_height="wrap_content"
  20. android:text="@string/content"/>
  21. <EditText
  22. android:id="@+id/content"
  23. android:layout_width="fill_parent"
  24. android:layout_height="wrap_content"
  25. android:layout_weight="0.96"
  26. android:inputType="textMultiLine"/>
  27. <Button
  28. android:id="@+id/button"
  29. android:layout_width="wrap_content"
  30. android:layout_height="wrap_content"
  31. android:text="@string/button"/>
  32. </LinearLayout>


这个文件主要用于设计界面,界面预览可以点击eclipse编辑文件区的Graphical Layout,如下所示:



●修改src/com/sinosoft目录下的SMSActivity.java文件

[java] view plain copy print ?
  1. packagecom.sinosoft;
  2. importandroid.app.Activity;
  3. importandroid.telephony.SmsManager;
  4. importandroid.widget.Toast;
  5. importandroid.os.Bundle;
  6. importandroid.widget.*;
  7. importandroid.view.*;
  8. importjava.util.*;
  9. publicclassSMSActivityextendsActivity{
  10. /**Calledwhentheactivityisfirstcreated.*/
  11. @Override
  12. publicvoidonCreate(BundlesavedInstanceState){
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.main);
  15. Buttonbuttion=(Button)this.findViewById(R.id.button);//获取按钮
  16. buttion.setOnClickListener(newView.OnClickListener(){//为按钮设置监听事件
  17. publicvoidonClick(Viewv){
  18. //TODOAuto-generatedmethodstub
  19. EditTextmobileText=(EditText)findViewById(R.id.mobile);//获得手机号码文本框
  20. EditTextcontentText=(EditText)findViewById(R.id.content);//获得短信文本框
  21. Stringmobile=mobileText.getText().toString();//获得手机号码
  22. Stringcontent=contentText.getText().toString();//获得短信内容
  23. SmsManagersmsManager=SmsManager.getDefault();//获取系统短信管理器
  24. List<String>list=smsManager.divideMessage(content);
  25. for(Stringl:list){//如果短信超过70个字,则将短信内容拆分为几条发送
  26. smsManager.sendTextMessage(mobile,null,l,null,null);
  27. }
  28. //Toast.makeText(SMSActivity.this,R.string.success,Toast.LENGTH_LONG).show();
  29. Toast.makeText(SMSActivity.this,R.string.sendSucc,Toast.LENGTH_LONG).show();//添加短信发送成功提醒
  30. }
  31. });
  32. }
  33. }


具体解释详见代码注释

●设置发送短信权限


在AndroidMainFest/xml加入一句:<uses-permissionandroid:name="android.permission.SEND_SMS"/>

就可,代码如下所示:

[html] view plain copy print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <manifestxmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.sinosoft"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. <uses-sdkandroid:minSdkVersion="10"/>
  7. <uses-permissionandroid:name="android.permission.SEND_SMS"/>
  8. <application
  9. android:icon="@drawable/ic_launcher"
  10. android:label="@string/app_name">
  11. <activity
  12. android:name=".SMSActivity"
  13. android:label="@string/app_name">
  14. <intent-filter>
  15. <actionandroid:name="android.intent.action.MAIN"/>
  16. <categoryandroid:name="android.intent.category.LAUNCHER"/>
  17. </intent-filter>
  18. </activity>
  19. </application>
  20. </manifest>

●运行程序


运行此程序后发送短信的界面如下所示:




输入“手机号码”与“短信内容”,如果发送成功,则会提示你“发送成功”:




这是接收到的手机短信:




很简单吧,其实实现了这2个小心愿之后我决定要回过头好好研究这个项目各个文件的作用,这是下节的开始的内容,小例子就学到这儿...


这是本人学习的结果,欢迎转载,欢迎交流,但转载务必给出本文章的链接地址:http://blog.csdn.net/youqishini/article/details/7371347,谢谢~


更多相关文章

  1. Android(安卓)Gradle开发
  2. 多种方式实现Android页面布局的切换
  3. Android内存情况
  4. Android(安卓)WebView文件上传各版本区别
  5. android 按钮点击更改背景色或背景图
  6. Android(安卓)Studio 布局文件格式化代码
  7. GmSSL Android端 :使用Android(安卓)Studio 调试Native
  8. Android中的文件下载
  9. Android导入工程出现 Project has no default.properties file!

随机推荐

  1. JS初识-变量及函数
  2. 支付回调代理转发
  3. 追格知识付费小程序系统源码正式发布 一
  4. CSS伪类选择器,引入字体图标,盒模型以及em
  5. 媒体查询,固定定位,flex属性
  6. rem+vw布局的原理,grid布局属性
  7. ubuntu20 源码安装nginx以及常用命令
  8. JavaScript中的流程控制与函数
  9. JS初识变量及函数学习
  10. html测试题小结