Android(安卓)---模拟手机发送短信
16lz
2021-01-26
上次成功实现了“拨打电话”后,心里兴奋极了,于是一口气研究了手机发送短信的原理,其实也很简单。
新建SMS,工程如下所示:
●修改res/values目录下的string.xml文件,如下所示:
[html] view plain copy print ?
- <?xmlversion="1.0"encoding="utf-8"?>
- <resources>
- <stringname="hello">HelloWorld,SMSActivity!</string>
- <stringname="app_name">发送短信</string>
- <stringname="mobile">请输入手机号</string>
- <stringname="content">请输入短信内容</string>
- <stringname="button">发送短信</string>
- <stringname="sendSucc">发送成功!</string>
- </resources>
●修改res/layout目录下的main/xml文件
[html] view plain copy print ?
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns: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文件
[java] view plain copy print ?
- packagecom.sinosoft;
- importandroid.app.Activity;
- importandroid.telephony.SmsManager;
- importandroid.widget.Toast;
- importandroid.os.Bundle;
- importandroid.widget.*;
- importandroid.view.*;
- importjava.util.*;
- publicclassSMSActivityextendsActivity{
- /**Calledwhentheactivityisfirstcreated.*/
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Buttonbuttion=(Button)this.findViewById(R.id.button);//获取按钮
- buttion.setOnClickListener(newView.OnClickListener(){//为按钮设置监听事件
- publicvoidonClick(Viewv){
- //TODOAuto-generatedmethodstub
- EditTextmobileText=(EditText)findViewById(R.id.mobile);//获得手机号码文本框
- EditTextcontentText=(EditText)findViewById(R.id.content);//获得短信文本框
- Stringmobile=mobileText.getText().toString();//获得手机号码
- Stringcontent=contentText.getText().toString();//获得短信内容
- SmsManagersmsManager=SmsManager.getDefault();//获取系统短信管理器
- List<String>list=smsManager.divideMessage(content);
- for(Stringl: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-permissionandroid:name="android.permission.SEND_SMS"/>
就可,代码如下所示:
[html] view plain copy print ?
- <?xmlversion="1.0"encoding="utf-8"?>
- <manifestxmlns:android="http://schemas.android.com/apk/res/android"
- package="com.sinosoft"
- android:versionCode="1"
- android:versionName="1.0">
- <uses-sdkandroid:minSdkVersion="10"/>
- <uses-permissionandroid: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>
- <actionandroid:name="android.intent.action.MAIN"/>
- <categoryandroid:name="android.intent.category.LAUNCHER"/>
- </intent-filter>
- </activity>
- </application>
- </manifest>
●运行程序
运行此程序后发送短信的界面如下所示:
输入“手机号码”与“短信内容”,如果发送成功,则会提示你“发送成功”:
这是接收到的手机短信:
很简单吧,其实实现了这2个小心愿之后我决定要回过头好好研究这个项目各个文件的作用,这是下节的开始的内容,小例子就学到这儿...
这是本人学习的结果,欢迎转载,欢迎交流,但转载务必给出本文章的链接地址:http://blog.csdn.net/youqishini/article/details/7371347,谢谢~
更多相关文章
- Android(安卓)Gradle开发
- 多种方式实现Android页面布局的切换
- Android内存情况
- Android(安卓)WebView文件上传各版本区别
- android 按钮点击更改背景色或背景图
- Android(安卓)Studio 布局文件格式化代码
- GmSSL Android端 :使用Android(安卓)Studio 调试Native
- Android中的文件下载
- Android导入工程出现 Project has no default.properties file!