用户登录
用户注册

分享至

Android短信发送器实现方法

  • 作者: 隔壁老王独霸后宫
  • 来源: 51数据库
  • 2021-11-22

本文实例讲述了android短信发送器实现方法。分享给大家供大家参考。具体如下:

这里模拟android短信发送器的实现

androidmanifest.xml清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.ljq.sms"
 android:versioncode="1"
 android:versionname="1.0">
 <application android:icon="@drawable/icon" android:label="@string/app_name">
 <activity android:name=".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>
 <uses-sdk android:minsdkversion="7" />
 <uses-permission android:name="android.permission.send_sms"/>
</manifest> 

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">
 <relativelayout android:layout_width="fill_parent"
 android:layout_height="wrap_content">
 <textview android:layout_width="115dip"
  android:layout_height="wrap_content" 
  android:text="请输入手机号"
  android:id="@+id/mobilelabel" />
 <edittext android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_torightof="@id/mobilelabel" 
  android:text="5556"
  android:id="@+id/mobile" />
 </relativelayout>
 <textview android:layout_width="fill_parent"
 android:layout_height="wrap_content" 
 android:text="请输入短信内容" />
 <edittext android:layout_width="fill_parent"
 android:layout_height="wrap_content" 
 android:minlines="3"
 android:text="i am a teacher!"
 android:id="@+id/content" />
 <button android:layout_width="wrap_content"
 android:layout_height="wrap_content" 
 android:text="发送"
 android:id="@+id/button" />
</linearlayout>

mainactivity类:

package com.ljq.sms;
import java.util.arraylist;
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 mainactivity extends activity {
 private edittext mobiletext=null;
 private edittext contenttext=null;
 @override
 public void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.main);
 mobiletext=(edittext)findviewbyid(r.id.mobile);
 contenttext=(edittext)findviewbyid(r.id.content);
 button button=(button)findviewbyid(r.id.button);
 button.setonclicklistener(new view.onclicklistener(){
  public void onclick(view v) {
  string mobile=mobiletext.gettext().tostring();
  string content=contenttext.gettext().tostring();
  //取得android系统中默认的短信管理器
  smsmanager manager=smsmanager.getdefault();
  //如果短信内容过长时,则对短信内容进行拆分
  arraylist<string> texts=manager.dividemessage(content);
  for(string text:texts){
   //第一个参数:对方手机号码
   //第二个参数:短信中心号码,一般设置为空
   //第三个参数:短信内容
   //第四个参数:sentintent判断短信是否发送成功,如果你没有sim卡,或者网络中断,则可以通过这个intent来判断。
   //注意强调的是“发送”的动作是否成功。那么至于对于对方是否收到,另当别论
   //第五个参数:当短信发送到收件人时,会收到这个deliveryintent。即强调了“发送”后的结果
   //就是说是在"短信发送成功"和"对方收到此短信"才会激活sentintent和deliveryintent这两个intent。这也相当于是延迟执行了intent
   manager.sendtextmessage(mobile, null, text, null, null);
  }
  //toast.maketext(getapplicationcontext(), "发送成功", toast.length_long).show();
  toast.maketext(mainactivity.this, "发送成功", toast.length_long).show();
  }
 });
 }
}

运行结果:

希望本文所述对大家的android程序设计有所帮助。

软件
前端设计
程序设计
Java相关