Android傻瓜开发实例-手机短信发送程序
一、用Eclipse创建 Android工程
基本信息如下:
Project name:SendMessage
BuildTarget:Android2.1
Application name:短信发送
Package name:my.android.sms
Create Activity:SendMessage
Min SDK Version:7
Eclipse系统会生成以下目录架构的源码和配置文件
源码目录架构:
│ AndroidManifest.xml
├─libs
├─res
│ ├─drawable-hdpi
│ │ icon.png
│ │
│ ├─drawable-ldpi
│ │ icon.png
│ │
│ ├─drawable-mdpi
│ │ icon.png
│ │
│ ├─layout
│ │ main.xml
│ │
│ └─values
│ strings.xml
│
└─src
└─my
└─android
└─sms
SendMessage.java
二、编辑工程相关文件:
主要涉及4个文件,分别是
res\values\strings.xml
res\layout\main.xml
src\my\android\sms\SendMessage.java
AndriodManifest.xml
具体修改内容如下:
1.编辑strings.xml文件内容为:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">请输入手机号码:</string>
<string name="app_name">发送短信</string>
<string name="content">请输入信息内容:</string>
<string name="send">发送</string>
</resources>
2.编辑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:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/mobile" />
<!-- 请输入信息内容标签 -->
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/content" />
<!-- 信息内容编辑框 -->
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content" android:minLines="3"
android:id="@+id/message" />
<!-- 发送按钮 -->
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/send"
android:id="@+id/send"/>
</LinearLayout>
注意,我们在电话号码输入框和拨打电话按钮中添加了android:id属性。如电话号码输入框的android:id=”@+id/mobile”,@代码R.java,+id代码添加id静态内部类,mobile代表向id类中添加一个常量成员。ADT将自动为我们生成常量值。
android:minLines设置信息内容编辑框的最小行数。
3.编辑SendMessage.java内容:
package my.android.sms;
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;
public class SendMessage extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 根据ID获取按钮
Button button = (Button) this.findViewById(R.id.send);
// 注册按钮被单击事件
button.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
// 根据ID获取手机号码编辑框
EditText mobileText = (EditText) findViewById(R.id.mobile);
// 获取手机号码
String mobile = mobileText.getText().toString();
// 根据ID获取信息内容编辑框
EditText messageText = (EditText) findViewById(R.id.message);
// 获取信息内容
String message = messageText.getText().toString();
// 移动运营商允许每次发送的字节数据有限,我们可以使用Android给我们提供 的短信工具。
if (message != null) {
SmsManager sms = SmsManager.getDefault();
// 如果短信没有超过限制长度,则返回一个长度的List。
List<String> texts = sms.divideMessage(message);
for (String text : texts) {
sms.sendTextMessage(mobile, null, text, null, null);
}
}//fi
}//public
});
}//public
}//public
-------------------------------------------------------------------------------------------------------------------
附:关键函数sendTextMessage参数说明:
sms.sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent):
destinationAddress:接收方的手机号码
scAddress:发送方的手机号码
text:信息内容
sentIntent:发送是否成功的回执,以后会详细介绍。
DeliveryIntent:接收是否成功的回执,以后会详细介绍。
4.编辑AndroidManifest.xml内容:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.android.sms" android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".SendMessage" 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>
<uses-sdk android:minSdkVersion="7" />
<!-- 注册发送短信的权限 -->
<uses-permission android:name="android.permission.SEND_SMS" />
</manifest>
注册发送短信的权限,如果没有注册这个,将使用不了系统的发送短信功能。以后在我们的应用程序开发中,有使用到系统功能的必须在这个文件中进行注册。我们可以查看Android的帮助手册都有哪些功能。(.../android-sdk-windows/docs/reference/android/Manifest.permission.html)
三、启动模拟器
我们给谁发短信?我们可以启动两个模拟器。使用一个模拟器给另一个模拟器发信息。首先我们使用工具栏上的手机图标再添加一个Android2.1的模拟器,另记一个名称。
在启动两个模拟器之前,我们需要模拟器能“接收到信号”。如果我们的机器是联网的,启动模拟器后,主界面显示信号强度的旁边会有一个3G的字样,这说明模拟器已经能接收到信号了。如果我们的机器不能联网,那么将自己的IP地址、网关和DNS服务器都设置为相同的值,比如都设置为192.168.0.100。如果我们的机器是在局域网下,但没有联网,那么将自己的网关和DNS设置为路由的IP即可,一般情况下路由的IP是192.168.0.1。
OK,现在我们启动两个模拟器!
四、发送短信
我们启动模拟器后,可以看到模拟器窗口的标题栏上有5554和55556的字样。这是模拟器监听的端口即——127.0.0.1:5554。
在工程上右键,Run As Android Application,选择其中的一个模拟器。比如选择了端口为5554的模拟器。OK,程序被加载到模拟器中了,会被自动运行。
我们在电话号码编辑框中输入5556(接收端模拟器的端口号),点击发送按钮!
OK,你看到效果了吗?5556主界面,显示信号强度的旁边显示收到新短信。
五、使用ADT插件发送短信给模拟器
如果机器太慢,无法启动两个模拟器,我们可以只启动一个模拟器。然后在菜单windows->show view->other->Android->Emulator Control打开Emulator Control面板。
Telephony Actions分组框中,Voice是呼叫,SMS是发送短信。Incoming number是模拟器的端口号,我们也可以使用这个功能给我们的模拟器拨打电话或发送短信。
分享到:
相关推荐
Android开发---发送短信程序 很实用的东西 看看在说吧
[14本经典Android开发教程]-6-Android驱动开发入门及手机案例开发分析教程 http://download.csdn.net/detail/cleopard/8388019 剩余8本稍后上传!@或直接从这里寻找@ http://download.csdn.net/user/cleopard/album...
[14本经典Android开发教程]-4-Android应用程序开发36技 本书共分为4篇: ※ 第一篇:介绍应用框架概念、原理和特性。 ※ 第二篇:阐述应用框架之设计技巧。亦即,如何打造应用框架。 (注:如果你的职务是「使用」...
Android项目实战--手机卫士10--GPS定位与发送短信指令
Android入门开发实例--Dialog 对话框应用
Android应用开发快速入门-.pdf
[14本经典Android开发教程]-11-Android...[14本经典Android开发教程]-6-Android驱动开发入门及手机案例开发分析教程 http://download.csdn.net/detail/cleopard/8388019 ....... 剩余3本稍后上传!@或直接从这里寻找@ ...
移动终端Android工程原代码,可直接安装运行,在手机上实现发送短消息的功能
Android开发入门101-HelloAndroid.pdf
[14本经典Android开发教程]-5-linux Android...[14本经典Android开发教程]-1-Android开发从入门到精通 http://download.csdn.net/detail/cleopard/8355245 [14本经典Android开发教程]-2-Android开发手册—API函数详解 ...
AndroidSerialPort ...涉及到的 jni 开发可以参考:【Android 应用开发】Android 开发 之 JNI入门 - NDK从入门到精通 jni 开发遇到的坑:https://github.com/jp1017/Android-Collection/issues/10
Android项目实战--手机卫士18--读取用户的短信内容以及短信备份
新版Android开发教程笔记--基础入门一
Android入门开发实例--Toast、Notification、Intent应用
[14本经典Android开发教程]-3-Android SDK 中文开发文档 什么是 Android? Android 是一个专门针对移动设备的软件集,它包括一个操作系统,中间件和一些重要的应用程序。Beta版的 Android SDK 提供了在Android 平台上...
新版Android开发教程&笔记--基础入门一 新版Android开发教程&笔记--基础入门二 新版Android开发教程&笔记三--环境搭建与解析 新版Android开发教程&笔记四--Dalvik ADB 新版Android开发教程+笔记五--模拟器、应用1、2...
Android开发入门101-HelloAndroid[参照].pdf
Android项目实战--手机卫士19--短信的恢复
简单的拼图游戏,供入门的朋友参考使用,主要包含src里面的4个.java文件对应应用的4个Activity。说明参考:http://blog.csdn.net/seanwang_25/article/details/22730633
Android App开发入门范例程序 Android App开发入门范例程序