Android短信发送程序
来源:互联网 发布:哪种理财软件好 编辑:程序博客网 时间:2024/05/01 05:26
转载请注明出处(http://blog.csdn.net/cndrip)
前一篇讲过拔号程序,这里讲一下短信发送程序。
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="fill_parent" android:layout_height="fill_parent" ><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /><EditText android:id="@+id/mobile" android:layout_width="match_parent" android:layout_height="wrap_content"> </EditText><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/textview"></TextView><EditText android:id="@+id/message" android:layout_width="match_parent" android:layout_height="wrap_content"></EditText><Button android:text="@string/Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button></LinearLayout>
string.xml
<?xml version="1.0" encoding="utf-8"?><resources> <string name="hello">请输入号码:</string> <string name="app_name">短信发送程序</string> <string name="textview">请输入短信内容:</string> <string name="Button">发送</string></resources>
添加发送短信的权限
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.drip.message" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <uses-permission android:name="android.permission.SEND_SMS"></uses-permission> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MessageActivity" 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>
MessageActivity.java
package com.drip.message;import java.util.List;import android.app.Activity;import android.os.Bundle;import android.telephony.SmsManager;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class MessageActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button button = (Button) this.findViewById(R.id.button1);button.setOnClickListener(new ButtonClickListener()); } private class ButtonClickListener implements OnClickListener { @Override public void onClick(View v){ EditText mobileText = (EditText) findViewById(R.id.mobile); String mobile = mobileText.getText().toString(); EditText messageText = (EditText) findViewById(R.id.message); String message = messageText.getText().toString(); SmsManager sm=SmsManager.getDefault(); if(messageText.length()>70){ //如果字数超过70,需拆分成多条短信发送 List<String> msgs = sm.divideMessage(message); for (String msg : msgs) { sm.sendTextMessage(mobile, null, msg, null, null); } }else { sm.sendTextMessage(mobile, null, message, null, null); } Toast.makeText(MessageActivity.this, "发送成功!", 1).show(); } } }
这里讲讲SmsManager类
Android是利用这个类进行短信处理,而文本则用这个sendTextMessage函数来完成
sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent)
destinationAddress:目标,即接收号码
scAddress:短信中心号码,测试时,就不需要了
text:要发送的文本
sentIntent:是否发送成功的状态
deliveryIntent:对方是否收到的状态
如何进行模拟器上测试,请详见Android手机拔号程序
本文的源码(http://download.csdn.net/detail/cndrip/3991864)
程序运行图:
- android 短信发送程序
- Android短信发送程序
- Android自制发送短信程序
- Android自制发送短信程序
- Android 发送短信小程序
- Android调用发送短信程序
- Android开发(一)发送短信程序
- Android开发(一)发送短信程序
- Android开发之发送短信程序
- Android调用拨号程序,短信发送
- Android外部监听类-发送短信程序
- 初识安卓小程序(Android短信发送器)
- Android发送短信的病毒程序
- ASP短信发送程序
- 短信发送程序
- 调用程序发送短信
- symbian 发送短信程序
- 发送短信小程序
- postgresql中query tree内存结构
- sql server 2008 空间数据类型的插入
- PHP中3种生成XML文件方法的速度比较
- 工作了才知道 我们把大学荒废了
- C语言成绩管理系统
- Android短信发送程序
- C语言之排学生成绩
- 口语语汇单词篇(13)
- 非递归遍历二叉树
- Oracle外键级联更新——延迟约束和触发器实现
- 常用正则表达式大全!(例如:匹配中文、匹配html)
- postgresql8.4+postgis1.5+eclipse CDT3.6 调试环境搭建
- PostGresSql Working with Eclipse
- 没有找到MFC42D.DLL