博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android之短信发送器
阅读量:6949 次
发布时间:2019-06-27

本文共 3371 字,大约阅读时间需要 11 分钟。

 

效果图:

界面布局:

[html] 
  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.     >  
  7.     <!--显示控件-->  
  8.     <TextView    
  9.     android:layout_width="fill_parent"   
  10.     android:layout_height="wrap_content"   
  11.     android:text="@string/moblie"  
  12.     />  
  13.     <!--文本框按钮-->  
  14.     <EditText  
  15.     android:layout_width="fill_parent"   
  16.     android:layout_height="wrap_content"  
  17.     android:id="@+id/moblie"  
  18.     />  
  19.     <TextView    
  20.     android:layout_width="fill_parent"   
  21.     android:layout_height="wrap_content"   
  22.     android:text="@string/content"  
  23.     />  
  24.     <EditText  
  25.     android:layout_width="fill_parent"   
  26.     android:layout_height="wrap_content"  
  27.     android:minLines="3"  
  28.     android:id="@+id/content"  
  29.     />  
  30.     <Button  
  31.     android:layout_width="wrap_content"   
  32.     android:layout_height="wrap_content"  
  33.     android:text="@string/button"  
  34.     android:id="@+id/button"  
  35.     />  
  36. </LinearLayout>  

接着是资源文件strings.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="info">免费短信发送成功</string> </resources>

Activity

import java.util.List; import android.app.Activity; import android.os.Bundle; import android.telephony.SmsManager; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class SmsActivity extends Activity { private EditText mobileText; //定义文本框 定义成员变量,就可以直接调用 private EditText contentText; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mobileText = (EditText)this.findViewById(R.id.mobile); contentText = (EditText)this.findViewById(R.id.content); Button button =(Button)this.findViewById(R.id.button);//按钮不需要在很多地方引用,所以就键个点击事件就搞定了, 后面不需要引用 button.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){ String mobile = mobileText.getText().toString(); String content = contentText.getText().toString(); SmsManager smsManager = SmsManager.getDefault(); //当信息长度超过120个字符,分多次发送 if(content.length() > 120){ List<String> contents = smsManager.divideMessage(content); for(String sms : contents){ //使用增强for循环 迭代短信内容 sms : contents smsManager.sendTextMessage(mobile,null,sms,null, null); } }else{ smsManager.sendTextMessage(mobile,null,content,null,null); }//采用吐西方式提示用户发送成功 Toast.makeText(SmsActivity.this, R.string.info, Toast.LENGTH_LONG).show(); //吐丝 //Toast是一种提供给用户简洁信息的视图 } }); } }

添加短信服务权限:

[html] 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="cn.itcast.action"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.       略....  
  7.     <uses-sdk android:minSdkVersion="8" />  
  8.     <!-- 短信服务权限 -->  
  9.     <uses-permission android:name="android.permission.SEND_SMS" />  
  10. </manifest>  

---------------------------------------------------------------------------------------

  smsManager.sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent)

  

如果不用,后面两个可以为null

--destinationAddress  目标电话号码,收报地址 

-- ScAddress 通讯录  服务商的短信中心号码(例如中国移动的短信中心号码),测试可以不填。

 -- sentIntent:发送(短信) -->中国移动 --> 中国移动发送失败 --> 返回发送成功或失败信号(广播出去) --> 后续处理   即,这个意图包装了短信发送状态的信息

   -- deliveryIntent:    广播(短信)接收器           即:这个意图包装了短信是否被对方收到的状态信息(供应商已经发送成功,但是对方没有收到)。//delivery 交付

转载地址:http://cshnl.baihongyu.com/

你可能感兴趣的文章
WordCount 远程集群源码
查看>>
java Date获取 年月日时分秒
查看>>
iOS 9应用开发教程之使用代码添加按钮美化按钮
查看>>
记一次服务器被恶意攻击的情况
查看>>
一个例子:HelloWorld
查看>>
排序算法及分析(插入、希尔、选择、冒泡)
查看>>
[转]Redmine 配置163邮箱
查看>>
C#--属性
查看>>
文本自动分割算法
查看>>
http://blog.csdn.net/i_bruce/article/details/39555417
查看>>
shell 调试手段总结
查看>>
CSharpGL(17)重构CSharpGL
查看>>
AVFoundation播放视频时显示字幕,切换音轨
查看>>
Spark笔记:复杂RDD的API的理解(上)
查看>>
java单例设计模式
查看>>
Druid.io索引过程分析——时间窗,列存储,LSM树,充分利用内存,concise压缩
查看>>
【emWin】例程十六:窗口管理器
查看>>
HTTP 403详解
查看>>
WPF实现在电脑重启或关机时执行某些逻辑
查看>>
PCA(主成分分析)的简单理解
查看>>