虽然手机内置了短信发送器,但是有时候为了特定的需求项目可能需要自己设计的短信发送器,下面是一个发送短信的小例子

首先是布局文件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"    />    <EditTextandroid:layout_width="fill_parent"     android:layout_height="wrap_content"    android:id="@+id/mobileCode" /><TextView      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="@string/content"    />    <EditTextandroid:layout_width="fill_parent"     android:layout_height="wrap_content"    android:minLines="4"    android:id="@+id/messageContent" /><Buttonandroid:layout_width="wrap_content"     android:layout_height="wrap_content"    android:text="@string/button"    android:id="@+id/sendButton"/></LinearLayout>


接着是资源文件strings.xml
<?xml version="1.0" encoding="utf-8"?><resources>    <string name="hello">请输入对方电话号码</string>    <string name="app_name">发送短信</string>     <string name="button">发送</string>     <string name="content">请输入短信内容</string>     <string name="status">发送成功</string></resources>


最后是处理响应的Activity
package com.lamp.activity;import java.util.List;import android.app.Activity;import android.os.Bundle;import android.telephony.SmsManager;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class SMSActivity extends Activity {public EditText mobileText = null;public EditText contentText = null;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                mobileText = (EditText)this.findViewById(R.id.mobileCode);        contentText = (EditText)this.findViewById(R.id.messageContent);        Button button = (Button)this.findViewById(R.id.sendButton);                button.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {String mobile = mobileText.getText().toString();String content = contentText.getText().toString();SmsManager sms = SmsManager.getDefault();//当信息长度超过40个字符,分多次发送if(content.length() > 40){List<String> contents = sms.divideMessage(content);for(String text : contents){sms.sendTextMessage(mobile, null, text, null, null);}}else{sms.sendTextMessage(mobile, null, content, null, null);}//信息发送完后的提示信息Toast.makeText(SMSActivity.this, R.string.status, Toast.LENGTH_SHORT).show();}});    }}


还有记得要在清单文件AndroidManifest.xml中注册发送短信的权限
<!-- 注册发送短信的权限 --><uses-permission android:name="android.permission.SEND_SMS" />

更多相关文章

  1. Android(安卓)launcher -- launcher源码修改 1
  2. 大家一起讨论简称论-关于简单Selector的制作与使用 (Production a
  3. 关于Android的app权限申请问题
  4. android 背景平铺[转]
  5. Android(安卓)AIDL 实例
  6. Android(安卓)studio 真机调试时生成的文件找不到的解决方案
  7. android 权限大全
  8. Android(安卓)获取通话记录和短信内容
  9. NPM 和webpack 的基础使用

随机推荐

  1. 【Android】Manifest中注册以内部类形式
  2. Android(安卓)平滑和立体翻页效果2
  3. Android环形统计控件
  4. Android--调用内置的浏览器
  5. Android(安卓)防止按钮重复点击
  6. Android源码中常用的系统广播
  7. Android提高十七篇之多级树形菜单的实现[
  8. Adapter 中getView使用注意点
  9. android 屏幕触摸事件的分发与处理
  10. 【读书笔记-《Android游戏编程之从零开始