Android实战--短信发送器
首先设计界面
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical"
> <EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入号码"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入内容"
/> </LinearLayout>

如果希望输入内容框变大变宽
可以设置
android:lines="6"

最后再添加button按钮
完整的代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical"
> <EditText
android:id="@+id/et_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入号码"
/>
<EditText
android:id="@+id/et_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入内容"
android:lines="6"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="send"
android:text="发送"
/> </LinearLayout>
界面如下:

点击发送后会发现报错,权限没设置

再新建一个模拟器avd2.3(版本低,运行快)

同时启动两个模拟器

注意:短信发送器的号码为5556
以上就完成了简单的发送短信功能
但是上面的操作还是存在一个小问题,那就是当短信的长度超过运营商限制的长度的时候,短信发送不出去
修改代码如下:
package com.wuyudong.smssender; import java.util.List; import android.os.Bundle;
import android.app.Activity;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.View;
import android.widget.EditText; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} public void send(View v) {
EditText et_phone = (EditText) findViewById(R.id.et_phone);
EditText et_content = (EditText) findViewById(R.id.et_content); String phone = et_phone.getText().toString();
String content = et_content.getText().toString(); // 获取短信管理器的对象
SmsManager sm = SmsManager.getDefault(); // 如果短信过长,分割短信,不能超过运营商限定的最长字符数
List<String> smss = sm.divideMessage(content); for (String sms : smss) {
// 发送短信
// arg0:对方的号码
// arg1:短信服务中心的号码,不要设置
// arg2:短信内容
sm.sendTextMessage(phone, null, sms, null, null);
}
}
}
搞定
Android实战--短信发送器的更多相关文章
- Android项目——短信发送器
因为应用要使用手机的短信服务,所以要在清单文件AndroidManifest.xml中添加短信服务权限: <?xml version="1.0" encoding=" ...
- Android(java)学习笔记99:android的短信发送器研究
1.第一种可以调用系统内部的短信程序. 之前我曾经出现过一个bug就是报错: android.content.ActivityNotFoundException: No Activity found ...
- 无废话Android之常见adb指令、电话拨号器、点击事件的4种写法、短信发送器、Android 中各种布局(1)
1.Android是什么 手机设备的软件栈,包括一个完整的操作系统.中间件.关键的应用程序,底层是linux内核,安全管理.内存管理.进程管理.电源管理.硬件驱动 2.Dalvik VM 和 JVM ...
- android开发学习---开发一个简易的短信发送器
一.需求: 开发一个简易的短信发送器,输入:对方手机号码,短信内容,点击发送按钮,短信发送成功,对方手机成功收到短信. 其中要求短信内容过长时可以自动拆分,长度英文是160个英文,中文是70个,中英混 ...
- Android短信发送器_08
1.string xml代码 <?xml version="1.0" encoding="utf-8"?> <resources> &l ...
- 初识安卓小程序(Android短信发送器)
首先,先创建一个安卓项目(我的版本号是4.4.2的),名字为"短信发送器" 然后在res目录下找到layout目录,找到activity_main.xml或fragment_mai ...
- Android短信发送器(2)
在上一篇的<Android短信发送器>当中.发送功能并不完好.当发送内容超过限定字数时,短信就会发送失败,此时就须要推断内容是否超过限制,假设不超过限制.就直接发送,反之.则对其进行处理再 ...
- Android学习4—短信发送器的实现
界面预览: 由图中可以看出,此APP需要的组件有:两个TextView,一个用于显示手机号码的标题,另一个用于显示短信内容的标题. ...
- Android_简易的短信发送器
这个随笔将介绍如何完成一个简单的第三方的短信发送器(不打开短信界面,调用android的api完成功能) 1.首先,我们来做布局 由于我这里写的是一个简易的,,短信发送,所以只是一个LinearLay ...
随机推荐
- 将main方法打成jar包,并引用第三方的maven jar包
一.准备工作.执行命令 学习插件: 学习apache的打包插件maven-assembly-plugin:http://maven.apache.org/plugins/maven-assembly- ...
- 安卓序列化漏洞 —— CVE-2015-3525
在2014年,Jann Horn发现一个安卓的提权漏洞,该漏洞允许恶意应用从普通应用权限提权到system用户执行命令,漏洞信息与POC见(1].漏洞的成因源于在安卓系统(<5.0)中,java ...
- 《Programming with Objective-C》
苹果官方文档:不稳定的传送门 读书笔记共有以下几篇,其他的知识点不重要或者已经熟悉不需记录 <Programming with Objective-C>第三章 Working with O ...
- MyBatis知多少(24)存储过程
使用MyBatis配置来调用存储过程.为了理解这一章,首先需要了解我们是如何在MySQL中创建一个存储过程. 在继续对本节学习之前,可以自行学习MySQL存储过程. 我们已经在MySQL下有EMPLO ...
- Unity3D内置着色器
Unity内部提供了一些可以直接使用的着色器,这些内置着色器包括以下6个方面: (1)Performance of Unity shaders 着色器的性能和两个方面有关:shader本身和rende ...
- Java Annotation认知(包括框架图、详细介绍、示例说明)
摘要 Java Annotation是JDK5.0引入的一种注释机制. 网上很多关于Java Annotation的文章,看得人眼花缭乱.Java Annotation本来很简单的,结果说的人没说清楚 ...
- [Math] A love of late toward Mathematics - how to learn it?
Link: https://www.zhihu.com/question/19556658/answer/26950430 王小龙 ,数学,计算机视觉,图形图像处理 数学系博士怒答! 我想大家 ...
- 【转载】关于Embedded Linux启动的经典问题
转载自:http://linux.chinaunix.net/techdoc/install/2009/04/13/1107608.shtml 发信人: armlinux (armlinux), 信区 ...
- Robot Framework自动化测试(四)--- 分层思想
谈到Robot Framework 分层的思想,就不得不提“关键字驱动”. 关键字驱动: 通过调用的关键字不同,从而引起测试结果的不同. 在上一节的selenium API 中所介绍的方法其实就是关 ...
- 免费素材下载:iOS 8 矢量 UI 素材套件
小伙伴们,苹果终于在今天凌晨推送了 iOS 8 的正式版.虽然该系统并未与 iPhone6 发布会同时亮相,但对于已经提前体验尝鲜过测试版的同学来说并不陌生.iOS 8 几乎每个图标都进行了重新设计, ...