第89章、系统服务之SMS服务(从零开始学Android)
每逢佳节,中国移动、电信、联通都是偷偷笑的日子,又不知道多少短信费用纳入囊中,尽管微信、QQ、飞信漫天飞,但仍然阻挡不了节日祝福短信的火爆,但群发实在没有意义,你是不是想来一个既个性而又群发呢?
譬如:“老夫子同学,你好!特祝愚人节快乐!”,按分类从联系人取出信息,然后加上名字和称呼,是不是这样的短信才更有价值与别具一格呢?
发送短信的关键程序是通过SmsManager对象的sendTextMessage()方法来完成,其中sendTextMessage()方法需传入五个值,依次是收件人地址(String),发送地址(String),发送服务(PendingIntent)与送达服务(PendingIntent),其中收件人与正文是不可为null的两个参数。
一、设计界面
1、布局文件
打开res/layout/activity_main.xml文件。
输入以下代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textphone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="手机号码:" />
<EditText
android:id="@+id/editphone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textcont"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="短信内容:" />
<EditText
android:id="@+id/editcont"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送" />
</LinearLayout>
二、程序文件
打开“src/com.genwoxue.sms/MainActivity.java”文件。
然后输入以下代码:
package com.genwoxue.sms;
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.app.Activity;
import android.app.PendingIntent;
public class MainActivity extends Activity {
private EditText editPhone=null;
private EditText editCont=null;
private Button btnSend=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editPhone=(EditText)super.findViewById(R.id.editphone);
editCont=(EditText)super.findViewById(R.id.editcont);
btnSend=(Button)super.findViewById(R.id.send);
btnSend.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
//实例化PendingIntent
PendingIntent intent=PendingIntent.getActivity(
MainActivity.this,
0,
MainActivity.this.getIntent(),
PendingIntent.FLAG_UPDATE_CURRENT);
//获取SmsManager服务
SmsManager sms=SmsManager.getDefault();
//发送短信
sms.sendTextMessage(
editPhone.getText().toString(), //手机号码
"13800371500", //短信中心号码
editCont.getText().toString(), //内容
intent,
null);
}
});
}
}
三、配置文件
打开“AndroidManifest.xml”文件。
然后输入以下代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.genwoxue.sms"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.genwoxue.sms.MainActivity"
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>
注意:需要在AndroidManifest.xml文件中添加权限:
<uses-permission android:name="android.permission.SEND_SMS"/>
四、运行结果
第89章、系统服务之SMS服务(从零开始学Android)相关推荐
- 第52章,bitmap图像处理(从零开始学android),第52章、Bitmap图像处理(从零开始学Android)...
1.Drawable → Bitmap public static Bitmap drawableToBitmap(Drawable drawable) { Bitmap bitmap = Bitma ...
- 第8章、下拉列表框Spinner(从零开始学Android)
在Android App应用中,下拉列表框Spinner的使用频次是相当高的,如果你对Spinner陌生,你一定不会对HTML中的SELECT陌生,他们的作用是一样的,都是多选一.我们需要学习Andr ...
- 第57章、录制视频(从零开始学Android)
录制视频显然要比音频复杂点,那么我们一起看看录制视频又该如何做呢? (1)首先,我们肯定要用到摄像头,因此需要在Manifest文件中声明使用权限: <uses-permission andro ...
- 从零开始学android:Android中的基本控件(上)
从零开始学android:Android中的基本控件(上) 本章内容较多,下面只贴代码,大家只需要贴到自己eclipse里就知道作用^^! View组件简介 Android中的View组件包含了几乎所 ...
- 第88章、系统服务之NOTIFICATION_SERVICE服务(从零开始学Android)
Notification通知将一个图标(包含一条可选的提示文本信息)填加到系统的状态栏(一般来说,Android手机的状态栏是在顶部,非底部,要注意噢)中,并将一条展开信息添加到通知窗口中.当用户选中 ...
- 从零开始学android编程_小白也能学得会!谷歌推出免费的Kotlin和Android开发课程...
程序员书库(ID:CodingBook) 猿妹编译 链接:https://android-developers.googleblog.com/2020/07/learn-android-and-kot ...
- 第1章、从零开始学Android
引子 滚滚长江东逝水 浪花淘尽英雄 是非成败转头空 青山依旧在 几度夕阳红 白发渔樵江渚上 惯看秋月春风 一壶浊酒喜相逢 古今多少事 都付笑谈中 --------------------------- ...
- 第15章、布局Layouts之TableLayout表格布局(从零开始学Android)
TableLayout表格布局 TableLayout是指将子元素的位置分配到行或列中.Android的一个TableLayout有许多TableRow组成,每一个TableRow都会定义一个Row. ...
- android 发送彩信监听,第74章、再识Intent-调用发送彩信程序(从零开始学Android)...
一.设计界面 1.布局文件 打开res/layout/activity_main.xml文件. 输入以下代码: xmlns:android="http://schemas.android.c ...
最新文章
- 程序人生:外链建设流程与细节都有哪些
- 最短路径的Dijkstra算法(邻接表)
- 图形基础 GPU架构(5)GPU vs CPU
- 阿里云Elasticsearch的X-Pack:机器学习、安全保障和可视化
- cognito_将Amazon Cognito与单页面应用程序(Vue.js)集成
- 倍福(Beckhoff)嵌入式控制器PLC
- cocos2d-x学习笔记之图片分辨率适配
- Eclipse快捷键以及设置
- 【Servlet】getInitParameter()发生空指针导致HTTP500的解决方案
- 彩票抽奖(洛谷P2550题题解,Java语言描述)
- Win10系统Win键无法使用
- mysql 执行计划 视频_MySQL执行计划的讲解
- 使用FFmpeg类库实现YUV视频序列编码为视频
- 你是一名技术管理者还是项目管理者?
- HTML5开发实战之网易微博
- Oracle nlssort()中文排序
- 汇总15位身份证号和18位身份证号的区别
- 微信小程序中类似后台添加商品规格的设置
- 【自动驾驶感知领域目前研究热点】
- redis sadd 命令 以及 删除set集合或者单独删除set中的一/多条记录