欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android基础开发小案例之短信发送器

程序员文章站 2024-03-03 17:14:46
先看看效果图: 布局文件: activity_main.xml

先看看效果图:

Android基础开发小案例之短信发送器

布局文件:
activity_main.xml

<span style="font-family:comic sans ms;font-size:14px;"><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=".mainactivity" > 
 
  <textview 
    android:id="@+id/textview1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignparenttop="true" 
    android:layout_centerhorizontal="true" 
    android:layout_margintop="17dp" 
    android:text="请输入手机号码:" 
    android:textsize="20dp" /> 
 
  <edittext 
    android:id="@+id/edittext1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/textview1" 
    android:layout_centerhorizontal="true" 
    android:layout_margintop="25dp" 
    android:background="@android:drawable/editbox_dropdown_light_frame" 
    android:ems="10" 
    android:inputtype="phone" 
    android:singleline="true" > 
 
    <requestfocus /> 
  </edittext> 
 
  <textview 
    android:id="@+id/textview2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignleft="@+id/textview1" 
    android:layout_below="@+id/edittext1" 
    android:layout_margintop="39dp" 
    android:text="请输入短信的内容:" 
    android:textsize="20dp" /> 
 
  <edittext 
    android:id="@+id/edittext2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/textview2" 
    android:layout_centerhorizontal="true" 
    android:layout_margintop="49dp" 
    android:background="@android:drawable/editbox_background" 
    android:ems="10" 
    android:hint="发送的内容..." 
    android:inputtype="textmultiline" 
    android:lines="5" /> 
 
  <button 
    android:id="@+id/btn_send" 
    android:layout_width="50dp" 
    android:layout_height="30dp" 
    android:layout_below="@+id/edittext2" 
    android:layout_centerhorizontal="true" 
    android:layout_margintop="30dp" 
    android:background="@drawable/reply_send_button" /> 
 
</relativelayout></span> 

java代码:

<span style="font-family:comic sans ms;font-size:14px;">package com.bzu.gxs; 
 
import java.util.arraylist; 
 
import android.os.bundle; 
import android.app.activity; 
import android.telephony.smsmanager; 
import android.text.textutils; 
import android.view.menu; 
import android.view.view; 
import android.view.view.onclicklistener; 
import android.widget.button; 
import android.widget.edittext; 
import android.widget.toast; 
 
public class mainactivity extends activity implements onclicklistener { 
  private edittext et_number; 
  private edittext et_content; 
  private button btn_send; 
 
  @override 
  protected void oncreate(bundle savedinstancestate) { 
    super.oncreate(savedinstancestate); 
    setcontentview(r.layout.activity_main); 
    et_number = (edittext) findviewbyid(r.id.edittext1); 
    et_content = (edittext) findviewbyid(r.id.edittext2); 
    btn_send = (button) findviewbyid(r.id.btn_send); 
 
    btn_send.setonclicklistener(this); 
  } 
 
  @override 
  public void onclick(view v) { 
    switch (v.getid()) { 
    case r.id.btn_send: 
      // 获取手机号码 
      string number = et_number.gettext().tostring().trim(); 
      // 获取短信内容 
      string content = et_content.gettext().tostring().trim(); 
      // 判断手机和短信的内容是否为空 
      if (textutils.isempty(content) || textutils.isempty(number)) { 
        toast.maketext(mainactivity.this, "手机号 或 短信内容 为空 ...", 
            toast.length_long).show(); 
        return; 
      } else { 
        smsmanager smsmanger = smsmanager.getdefault(); 
        // 把短信拆分成多个片段,防止短信内容过长,发送失败 
        arraylist<string> contents = smsmanger.dividemessage(content); 
        // 遍历短信内容 
        for (string str : contents) { 
          /* 
           * smsmanger.sendtextmessage(destinationaddress, scaddress, text, sentintent, deliveryintent) 
           * sendtextmessage方法的 
           * 第一个参数是信息的接收者 
           * 第二个参数是短信来自于哪里,目前不支持填写null就可以 
           * 第三个参数短信发送的内容 
           * 第四个参数是判断短信是否发送成功 
           * 第五个参数是对面接收到你发的短信的一个消息报告 
           */ 
          smsmanger 
              .sendtextmessage(number, null, content, null, null); 
          toast.maketext(mainactivity.this, "发送成功...", 
              toast.length_long).show(); 
        } 
      } 
      break; 
    } 
  } 
} 
</span> 

以上就是android短信发送器的实现代码,希望能给大家一个参考,也希望大家多多支持。