Android短信发送程序

来源:互联网 发布:哪种理财软件好 编辑:程序博客网 时间:2024/05/01 05:26

转载请注明出处(http://blog.csdn.net/cndrip)

 前一篇讲过拔号程序,这里讲一下短信发送程序。
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"    /><EditText android:id="@+id/mobile" android:layout_width="match_parent" android:layout_height="wrap_content"> </EditText><TextView android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="@string/textview"></TextView><EditText android:id="@+id/message" android:layout_width="match_parent" android:layout_height="wrap_content"></EditText><Button android:text="@string/Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button></LinearLayout>


string.xml

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="hello">请输入号码:</string>    <string name="app_name">短信发送程序</string>    <string name="textview">请输入短信内容:</string>    <string name="Button">发送</string></resources>

添加发送短信的权限
AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="com.drip.message"      android:versionCode="1"      android:versionName="1.0">    <uses-sdk android:minSdkVersion="8" />    <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>    <application android:icon="@drawable/icon" android:label="@string/app_name">        <activity android:name=".MessageActivity"                  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>

MessageActivity.java

package com.drip.message;import java.util.List;import android.app.Activity;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.widget.Toast;public class MessageActivity extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        Button button = (Button) this.findViewById(R.id.button1);button.setOnClickListener(new ButtonClickListener());    }    private class ButtonClickListener implements OnClickListener {       @Override     public void onClick(View v){     EditText mobileText = (EditText) findViewById(R.id.mobile);               String mobile = mobileText.getText().toString();              EditText messageText = (EditText) findViewById(R.id.message);               String message = messageText.getText().toString();              SmsManager sm=SmsManager.getDefault();             if(messageText.length()>70){            //如果字数超过70,需拆分成多条短信发送            List<String> msgs = sm.divideMessage(message);                 for (String msg : msgs) {                     sm.sendTextMessage(mobile, null, msg, null, null);                                                          }              }else {                 sm.sendTextMessage(mobile, null, message, null, null);                                   }              Toast.makeText(MessageActivity.this, "发送成功!", 1).show();          }        }    }


这里讲讲SmsManager类

Android是利用这个类进行短信处理,而文本则用这个sendTextMessage函数来完成

sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent)

destinationAddress:目标,即接收号码
scAddress:短信中心号码,测试时,就不需要了
text:要发送的文本
sentIntent:是否发送成功的状态
deliveryIntent:对方是否收到的状态

如何进行模拟器上测试,请详见Android手机拔号程序

 

本文的源码(http://download.csdn.net/detail/cndrip/3991864)
                


程序运行图:

原创粉丝点击