Android学习笔记(5)---模拟手机发送短信
来源:互联网 发布:莱特币源码分析 编辑:程序博客网 时间:2024/05/05 07:51
上次成功实现了“拨打电话”后,心里兴奋极了,于是一口气研究了手机发送短信的原理,其实也很简单。
新建SMS,工程如下所示:
●修改res/values目录下的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="sendSucc">发送成功!</string></resources>
●修改res/layout目录下的main/xml文件
<?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/mobile" /> <EditText android:id="@+id/mobile" android:layout_width="fill_parent" android:layout_height="wrap_content"> <requestFocus /> </EditText> <TextView android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/content" /> <EditText android:id="@+id/content" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0.96" android:inputType="textMultiLine"/> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button" /> </LinearLayout>
这个文件主要用于设计界面,界面预览可以点击eclipse编辑文件区的Graphical Layout,如下所示:
●修改src/com/sinosoft目录下的SMSActivity.java文件
package com.sinosoft; import android.app.Activity;import android.telephony.SmsManager;import android.widget.Toast;import android.os.Bundle;import android.widget.*;import android.view.*;import java.util.*;public class SMSActivity extends Activity { /** Calledwhen the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button buttion=(Button) this.findViewById(R.id.button); //获取按钮 buttion.setOnClickListener(newView.OnClickListener() { //为按钮设置监听事件 public void onClick(View v) { // TODO Auto-generatedmethod stub 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 smsManager=SmsManager.getDefault(); //获取系统短信管理器 List<String> list=smsManager.divideMessage(content); for(String l:list){ //如果短信超过70个字,则将短信内容拆分为几条发送 smsManager.sendTextMessage(mobile, null, l, null, null); } // Toast.makeText(SMSActivity.this,R.string.success, Toast.LENGTH_LONG).show(); Toast.makeText(SMSActivity.this, R.string.sendSucc, Toast.LENGTH_LONG).show(); //添加短信发送成功提醒 } }); }}
具体解释详见代码注释
●设置发送短信权限
在AndroidMainFest/xml加入一句:<uses-permission android:name="android.permission.SEND_SMS"/>
就可,代码如下所示:
<?xml version="1.0"encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.sinosoft" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10"/> <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>
●运行程序
运行此程序后发送短信的界面如下所示:
输入“手机号码”与“短信内容”,如果发送成功,则会提示你“发送成功”:
这是接收到的手机短信:
很简单吧,其实实现了这2个小心愿之后我决定要回过头好好研究这个项目各个文件的作用,这是下节的开始的内容,小例子就学到这儿...
这是本人学习的结果,欢迎转载,欢迎交流,但转载务必给出本文章的链接地址:http://blog.csdn.net/youqishini/article/details/7371347,谢谢~
- Android学习笔记(5)---模拟手机发送短信
- Android ---模拟手机发送短信
- Android模拟手机QQ+发送短信案例
- Android(OPhone) 学习笔记 - 短信发送
- Android 手机发送短信
- Android学习笔记(2)——短信发送器
- android学习笔记3--------------短信发送器
- Android学习笔记_2_发送短信
- 学习笔记7-Android短信发送器
- Android手机自动发送短信
- Androidx学习笔记(5)--短信发送器
- Android学习(8) -- 短信发送器
- Android学习笔记(4)---模拟手机拨号器
- Android 短信发送接收笔记
- Android学习笔记---08_短信发送器的制作
- Android源码学习笔记1-短信发送流程分析
- 【Android】安卓学习笔记之发送短信
- android模拟打电话和发送短信
- 通用新增(批量 实体)
- Linux下PostgreSQL安装与开机启动
- 单向链表C语言实现
- 搬家了
- 常见异常流量及蠕虫案例
- Android学习笔记(5)---模拟手机发送短信
- Linux下V4L2编程小结
- 工厂模式的形象理解
- 哈弗曼编码C语言实现
- 认真学习下字符串和字符数组的区别!
- ps基础知识学习总结
- MSDN 上的Hook例子2
- 第7章 布局(二)
- 耶鲁大学心态:送给正在奋斗的人