Android 中发送和接收短信
来源:互联网 发布:淘宝靠谱的男装韩代 编辑:程序博客网 时间:2024/05/02 02:23
在做Android开发中经常要用到短信的发送和短信的接收,调用Android提供的api实现起来很简单,今天要用到这个功能研究了一下顺便写下来加强一下记忆。
1.首先创建一个Android Project
2.设计一个main Activity作为发短信的操作界面,设计好的界面图如下:
界面布局的文件内容如下:复制代码3.创建一个Java类文件,导入以下包:复制代码4.重写onCreate方:复制代码
1.首先创建一个Android Project
2.设计一个main Activity作为发短信的操作界面,设计好的界面图如下:
界面布局的文件内容如下:
- 1: <?xml version="1.0" encoding="utf-8"?>
- 2: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- 3: android:orientation="vertical"
- 4: android:layout_width="fill_parent"
- 5: android:layout_height="fill_parent"
- 6: android:padding="10sp"
- 7: >
- 8: <TextView
- 9: android:layout_width="fill_parent"
- 10: android:layout_height="wrap_content"
- 11: android:text="@string/mobile_label"
- 12:
- 13: />
- 14: <EditText
- 15: android:layout_width="fill_parent"
- 16: android:layout_height="wrap_content"
- 17: android:id="@+id/txt_from"
- 18: android:hint="请输入电话号码"
- 19:
- 20: />
- 21: <TextView
- 22: android:layout_width="fill_parent"
- 23: android:layout_height="wrap_content"
- 24: android:text="@string/content_label"
- 25: />
- 26: <EditText
- 27: android:layout_width="fill_parent"
- 28: android:layout_height="wrap_content"
- 29: android:id="@+id/txt_content"
- 30: android:hint="请输入短信内容"
- 31: android:lines="3"
- 32: />
- 33: <TextView android:layout_width="fill_parent"
- 34: android:layout_height="wrap_content"
- 35: ></TextView>
- 36: <Button
- 37: android:text="发送短信"
- 38: android:layout_width="fill_parent"
- 39: android:layout_height="wrap_content"
- 40: android:gravity="center"
- 41: android:id="@+id/btnSend"
- 42: android:paddingTop="20sp"
- 43: />
- 44: </LinearLayout>
- 1: import java.util.regex.Matcher;
- 2: import java.util.regex.Pattern;
- 3: import com.eshore.smsManagers.R;
- 4: import android.app.Activity;
- 5: import android.telephony.gsm.*;
- 6: import android.view.View;
- 7: import android.view.View.OnClickListener;
- 8: import android.widget.Button;
- 9: import android.widget.EditText;
- 10: import android.widget.Toast;
- 11: import android.os.Bundle;
- 1: txtFrom=(EditText)this.findViewById(R.id.txt_from);
- 2: txtContent=(EditText)this.findViewById(R.id.txt_content);
- 3: btnSend=(Button)this.findViewById(R.id.btnSend);
- 4: btnSend.setOnClickListener(new OnClickListener() {
- 5: public void onClick(View v) {
- 6: if(!validate())
- 7: return;
- 8: SmsManager.getDefault().sendTextMessage(txtFrom.getText().toString().trim(),null,txtContent.getText().toString(),null,null);
- 9: txtFrom.setText("");
- 10: txtContent.setText("");
- 11: Toast toast=Toast.makeText(main.this, "短信发送成功!",Toast.LENGTH_LONG);
- 12: toast.show();
- 13: }
- 14: });
相关的辅助方法有手机的合法性验证和短信内容不可为空:复制代码经过上面的几个步骤发短信的功能基本就完成了,但是现在运行程序是无法工作的,主要是配置文件AndroidManifest.xml中的权限没有配置,要发送短信就要配置,发送短信的权限这样Android才会发送信息,否则发不出去信息,同样接收信息也需要有相应的接收短信的权限,在后面还要做接收短信的内容,所以在这里顺便将接收和发送短信的权限都配置好,配置代买如下:复制代码可以看出来第一行是发送短信的权限,第二行是接收短信的权限
运行程序,填写正确的手机号和短信内容点击发送就可以将短信内容发送到相应的手机号上。
5、接收短信,接收短信稍有点复杂,首先创建一个接收短信的Java类文件“ReceiverDemo.java”并继承”BroadcastReceiver”
6.重写下面的方法:复制代码重写内容如下:复制代码注意:
A:第7行代码用于判断当前监听的是否是接收短信这个事件,如果是则进行下面的处理,否则不做处理
B:12到22行解析出发信人的号码和短信内容并组成一个可读的字符串
C:22和23行将上面组成的字符串显示给用户
7.在AndroidManifest.xml中配置一个Receiver如下:复制代码
- 1: //合法性验证
- 2: private boolean validate(){
- 3: String mobile=txtFrom.getText().toString().trim();
- 4: String content=txtContent.getText().toString();
- 5: if(mobile.equals("")){
- 6: Toast toast=Toast.makeText(this, "手机号码不能为空!",Toast.LENGTH_LONG);
- 7: toast.show();
- 8: return false;
- 9: }
- 10: else if(!checkMobile(mobile)){
- 11: Toast toast=Toast.makeText(this, "您输入的电话号码不正确请重新输入!",Toast.LENGTH_LONG);
- 12: toast.show();
- 13: return false;
- 14: }
- 15: else if(content.equals("")){
- 16: Toast toast=Toast.makeText(this, "短信内容不能为空请重新输入!",Toast.LENGTH_LONG);
- 17: toast.show();
- 18: return false;
- 19: }
- 20: else{
- 21: return true;
- 22: }
- 23:
- 24: }
- 25: /*
- 26: * 中国移动134.135.136.137.138.139.150.151.152.157.158.159.187.188 ,147(数据卡不验证)
- 27: 中国联通130.131.132.155.156.185.186
- 28: 中国电信133.153.180.189
- 29: CDMA 133,153
- 30: 手机号码验证 适合目前所有的手机
- 31: */
- 32: public boolean checkMobile(String mobile){
- 33: String regex="^1(3[0-9]|5[012356789]|8[0789])\\d{8}[ DISCUZ_CODE_10 ]quot;;
- 34: Pattern p = Pattern.compile(regex);
- 35: Matcher m = p.matcher(mobile);
- 36: return m.find();
- 37: }
- 1: <uses-permission android:name="android.permission.SEND_SMS"/>
- 2: <uses-permission android:name="android.permission.RECEIVE_SMS"/>
运行程序,填写正确的手机号和短信内容点击发送就可以将短信内容发送到相应的手机号上。
5、接收短信,接收短信稍有点复杂,首先创建一个接收短信的Java类文件“ReceiverDemo.java”并继承”BroadcastReceiver”
6.重写下面的方法:
- 1: public void onReceive(Context context, Intent intent)
- 1: private static final String strRes = "android.provider.Telephony.SMS_RECEIVED";
- 2: @Override
- 3: public void onReceive(Context context, Intent intent) {
- 4: /*
- 5: * 判断是否是SMS_RECEIVED事件被触发
- 6: */
- 7: if (intent.getAction().equals(strRes)) {
- 8: StringBuilder sb = new StringBuilder();
- 9: Bundle bundle = intent.getExtras();
- 10: if (bundle != null) {
- 11: Object[] pdus = (Object[]) bundle.get("pdus");
- 12: SmsMessage[] msg = new SmsMessage[pdus.length];
- 13: for (int i = 0; i < pdus.length; i++) {
- 14: msg = SmsMessage.createFromPdu((byte[]) pdus);
- 15: }
- 16: for (SmsMessage currMsg : msg) {
- 17: sb.append("您收到了来自:【");
- 18: sb.append(currMsg.getDisplayOriginatingAddress());
- 19: sb.append("】\n的信息,内容:");
- 20: sb.append(currMsg.getDisplayMessageBody());
- 21: }
- 22: Toast toast = Toast.makeText(context, "收到了短消息: " + sb.toString(),Toast.LENGTH_LONG);
- 23: toast.show();
- 24: }
- 25: }
- 26: }
A:第7行代码用于判断当前监听的是否是接收短信这个事件,如果是则进行下面的处理,否则不做处理
B:12到22行解析出发信人的号码和短信内容并组成一个可读的字符串
C:22和23行将上面组成的字符串显示给用户
7.在AndroidManifest.xml中配置一个Receiver如下:
- 1: <receiver android:name=".ReceiverDemo" android:enabled="true">
- 2: <intent-filter>
- 3: <action android:name="android.provider.Telephony.SMS_RECEIVED" />
- 4: </intent-filter>
- 5: </receiver>
- Android中发送和接收短信
- Android 中发送和接收短信
- Android中发送和接收短信
- android接收和发送短信
- Android 接收和发送短信
- Android接收和发送短信
- Android接收和发送短信
- android中如何发送短信、接收短信
- Android接收短信和发送短信
- android中短信的发送与接收
- android 短信的接收和发送处理
- android 短信的接收和发送处理
- Android-短信的接收和发送
- android短信的接收和发送处理
- Android之接收和发送短信
- android短信接收处理和发送
- Android开发之接收和发送短信
- android广播监听接收和发送短信
- java的几种对象(PO,VO,DAO,BO,POJO)解释
- apache2.2.21+tomcat6+mod_jk在Windows中配置多域名对应单独IP虚拟主机
- Mac 常用软件注册码
- 程序员需要名校吗
- ListView滑动时 item出现黑色背景的问题的解决方法
- Android 中发送和接收短信
- lucene之Document之人为设置相关度得分
- unix线程消息队列
- 安装CE6.0SDK失败(转)
- VB6 GDI+ 入门教程(6)——图片
- Sqlite详解1
- Uva 11549(计算机谜题)(模拟)
- Linux内核0.12——理清内核相关知识
- 我的七个建议