Android-SendSMS
来源:互联网 发布:10年选秀体测数据 编辑:程序博客网 时间:2024/06/04 01:20
SmsManager
简介
SmsManager(短信管理器)是Android提供的另一个非常常见的服务,用于管理手机短信,一般用于实现在app中发送短信的功能SmsManager提供了系列sendXxxMessage()方法用于发送短信。
使用方法
调用SmsManager提供的短信接口sendTextMessage函数即可。
public void sendTextMessage (String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)
用到的参数:
- String destinationAddress 收信人电话号码
- String scAddress 短信中心的号码,一般用null表示默认
- String text 短信的内容
- PendingIntent sentIntent 短信发送状态的信息,一般用null
- PendingIntent deliveryIntent 对方接收状态的信息,一般用null
源代码
MainActivity.java
package com.example.administrator.sendsms;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.telephony.SmsManager;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;import java.util.ArrayList;public class MainActivity extends AppCompatActivity { EditText numberET,contentET;//numberEditText,contentEditText Button send; SmsManager sMananger; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 获取SmsManager sMananger = SmsManager.getDefault(); // 获取程序界面上的两个文本框和按钮 numberET = (EditText) findViewById(R.id.number); contentET = (EditText) findViewById(R.id.content); send = (Button) findViewById(R.id.send); send.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String phone = numberET.getText().toString(); String context = contentET.getText().toString(); ArrayList<String> list = sMananger.divideMessage(context); //因为一条短信有字数限制,因此要将长短信拆分 for(String text:list) { sMananger.sendTextMessage(phone,null,text,null,null); } Toast.makeText(getApplicationContext(),"发送完毕",Toast.LENGTH_SHORT).show(); } }); }}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/number" /> <EditText android:id="@+id/number" android:layout_height="wrap_content" android:layout_width="match_parent" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/content" /> <EditText android:id="@+id/content" android:layout_width="match_parent" android:layout_height="wrap_content" android:lines="2" /> <Button android:id="@+id/send" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/app_name" android:layout_gravity="center_horizontal" /></LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.administrator.sendsms"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> //获取系统权限,若没有添加下面这句代码,发送短信时会闪退 <uses-permission android:name="android.permission.SEND_SMS" /></manifest>
strings.xml(res/values/strings.xml)
<resources> <string name="app_name">04-SendSMS</string> <string name="hello">Hello World, SendSms!</string> <string name="number">收件人号码:</string> <string name="content">短信内容:</string> <string name="send">发送</string></resources>
注意事项
关于SmsManager
首先要通过getDefault方法获取SmsManager:
// 获取SmsManagersMananger = SmsManager.getDefault();
获取了SmsManager之后,调用了divideMessage方法分割长短信:
ArrayList<String> list = sMananger.divideMessage(context);
之后调用sendTextMessage方法发送短信:
sMananger.sendTextMessage(phone,null,text,null,null);
关于String.xml
此例中String.xml的代码如下:
<resources> <string name="app_name">04-SendSMS</string> <string name="hello">Hello World, SendSms!</string> <string name="number">收件人号码:</string> <string name="content">短信内容:</string> <string name="send">发送</string></resources>
从xml语法的角度分析,resources拥有元素内容,因为它包含了其他元素(若干个string),而这若干个string元素除了拥有属性app_name、hello等等,又包含了文本内容。
我们再看activity_main.xml中关于某个TextView的代码:
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/number" />
这个TextView的android:text属性是@string/number,意思是属性为number的一个string。也就是说,在执行activity_main.xml文件时,它会自动去资源库String.xml里寻找需要的内容。这意味着,如果按照这种方式写,当我们修改String.xml里的内容时,就可以很快改掉activity_main.xml中的内容。
关于divideMessage
一条短信只可容纳70个中文,所以当短信长度超过70个中文字符时程序就要特殊处理了。divideMessage主要用于处理这种情况。
有两种方式可以处理,一种是本例中用到的,用sendTextMessage方法:
if (message.length() > 70) { ArrayList<String> msgs = sms.divideMessage(message); for (String msg : msgs) { sms.sendTextMessage(phoneNumber, null, msg, null, null); }} else { sms.sendTextMessage(phoneNumber, null, message, sentPI, deliverPI); }
一种是用sendMultipartTextMessage方法:
if (message.length() > 70) { ArrayList<String> msgs = sms.divideMessage(message); ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>(); for(int i = 0;i<msgs.size();i++) { sentIntents.add(sentPI); } sms.sendMultipartTextMessage(phoneNumber, null, msgs, sentIntents, null);} else { sms.sendTextMessage(phoneNumber, null, message, sentPI, deliverPI); }
- Android-SendSMS
- 短信发送API(SendSms)---PHP
- 阿里-短信发送API(SendSms)---JAVA
- Android
- android
- Android
- android
- android
- Android
- Android
- android
- android
- android
- Android
- Android
- android!!!
- android
- android
- mac OS搭建C++ CLion开发环境
- Linux 学习历程(1)安装、屏幕输出、卡机
- 优秀开源项目之五:茶绘君
- iOS开发—使用NSOperation子类操作
- 如何定义一个高逼格的原生JS插件
- Android-SendSMS
- 5.1 Object类型
- 杨辉三角
- Copyright 格式
- 算法爱好者——乘积最大子序列 ? 待解决
- php开发公众号,接收用户发送的视频
- 可变参数列表源码的剖析
- LeetCode解题-#1-Two sum
- 推箱子 小时候困扰我多时的问题 只会过前两关的恐惧终于被电脑克服了