1. 创建android项目
  2. 创建界面布局
    1. TextView文本
    2. EditText电话号码及姓名输入框
    3. 发送短信按钮
  3. 处理发生短信请求
    1. 获取控件
    2. 给发生短信按钮设置点击事件
      1. 获取短信号码及内容
      2. 判断短信号码及内容是否为空
      3. 获取短息的管理器对象
      4. 如果你的字符数大于了70拆分
      5. 发送短信的处理
  4. 添加发送短信的权限

strings.xml

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">lession01_sms</string>    <string name="action_settings">Settings</string>    <string name="phone_num">请输入手机号码</string>    <string name="phone_content">请输入发送内容</string>    <string name="phone_button">发送短信</string></resources>


布局文件:activity_sms.xml

<RelativeLayout 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=".SmsActivity" >    <TextView        android:id="@+id/view_phone"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/phone_num" />    <EditText         android:id="@+id/edit_num"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:inputType="phone"        android:layout_below="@+id/view_phone"        android:layout_alignParentLeft="true" />    <TextView        android:id="@+id/view_content"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/phone_content"        android:layout_below="@+id/edit_num" />    <EditText         android:id="@+id/edit_content"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:inputType="text"        android:minLines="5"        android:layout_below="@+id/view_content"/>    <Button        android:id="@+id/button_send"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/phone_button"        android:layout_below="@+id/edit_content"/></RelativeLayout>


添加权限


SmsActivity.java

package com.example.lession01_sms;import java.util.ArrayList;import android.os.Bundle;import android.app.Activity;import android.app.PendingIntent;import android.content.Intent;import android.telephony.SmsManager;import android.view.Menu;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class SmsActivity extends Activity {// 声明控件对象public Button btn_send;public EditText edit_num;public EditText edit_content;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// 设置显示的视图(布局文件)setContentView(R.layout.activity_sms);// 获取控件对象btn_send = (Button) findViewById(R.id.button_send);edit_num = (EditText) findViewById(R.id.edit_num);edit_content = (EditText) findViewById(R.id.edit_content);// 为按钮控件注册事件btn_send.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View arg0) {// 获取电话号码String phone_num = edit_num.getText().toString();// 获取发送内容String phone_content = edit_content.getText().toString();// 获取短信管理器对象SmsManager smsManager = SmsManager.getDefault();// 意图对象PendingIntent pendingIntent = PendingIntent.getBroadcast(SmsActivity.this, 0, new Intent(), 0);//70分页ArrayList<String> contents = smsManager.divideMessage(phone_content);for(String c :contents){smsManager.sendTextMessage(phone_num, null, c, pendingIntent, null);}// 发送信息操作smsManager.sendTextMessage(phone_num, null, phone_content,pendingIntent, null);// 吐司效果Toast.makeText(SmsActivity.this, "发送完毕", Toast.LENGTH_LONG).show();}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.sms, menu);return true;}}

android模拟器5554给5556发送短信



模拟器5556接受短信




当5554给5556发送大于70的短信时进行自动拆分


当5556接收大于70字符的短信时,收到的内容将会自动拆分显示





更多相关文章

  1. Android(安卓)UI 设计——TextView 控件
  2. Android实现使用微信登录第三方APP的方法
  3. Android中Java根据文件头获取文件类型的方法
  4. Android如何获取系统联系人数据?
  5. android 你或许不知道的屏幕适配
  6. Android上定义播放器控件UniversalVideoView
  7. android 自定义控件 自定义属性详细介绍
  8. 从iOS角度思考Android
  9. Android(安卓)Studio生成签名文件,自动签名,以及获取SHA1和MD5值

随机推荐

  1. (亲测可用!!)android studio 编译 'com.andro
  2. 实现简单的Android的播放视频功能
  3. Android中进行HTTP操作
  4. [转]Android(安卓)Audio简述
  5. 在Android中支持表情
  6. Ubuntu 下配置使用Android(安卓)adb
  7. /system/bin/service分析与使用
  8. Android上的log,日志相关
  9. Android自定义相机,切换前后摄像头,照相
  10. Android(安卓)让你的 EditText 只接受指