发送短信,识别短信内容字符在70以内,然后电话收件人字符的确定

发送短信的关键是通过SmsManager对象的sendTextMessage()方法来实现的,其中sendTextMessage() 方法需要传入5个值,依次是收件人地址(String),发送地址(String) 正文内容(String)发送服务PendingIntent和送达服务(PendingIntent)其中收件人与正文是不可为Null的两个参数

 

 

定义个一个PendingIntent,getBroacast()的方法自定义PendingIntent并进行Broadcast,而后使用SmsManager。getDefault(当处理SMS短信相关的活动,例如发送数据,文字与pdu SMS 信息,都需要调用这种静态方法)所预先构建的SmsManager使用sendTextMessage()方法,将相关数据以参数形式带入既可以完成送达任务

 

首先定义一个Activity

package cn.mw.com.sms;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class EX01_03Activity extends Activity {
	EditText ed_shoujian, ed_content;
	Button btn_send;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		ed_shoujian = (EditText) findViewById(R.id.ed_shoujian);
		ed_content = (EditText) findViewById(R.id.ed_content);
		btn_send = (Button) findViewById(R.id.btn_send);
		ed_shoujian.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				ed_shoujian.setText("");
			}
		});
		ed_content.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				ed_content.setText("");
			}
		});
		btn_send.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// 获取收件人的信息
				String strAddress = ed_shoujian.getText().toString();
				// 获取短信的信息
				String strMessage = ed_content.getText().toString();
				// 创建一取得defaultinstance 的SmsManager对象
				SmsManager sm = SmsManager.getDefault();
				// 检查收件人电话格式与短信字数是否超过七十个字符
				if (isPhoneNumberValid(strAddress) == true
						&& iswithin70(strMessage) == true) {
					try {
						PendingIntent mPI = PendingIntent.getBroadcast(
								getApplicationContext(), 0, new Intent(), 0);
						sm.sendTextMessage(strAddress, null, strMessage, mPI,
								null);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					Toast.makeText(getApplicationContext(), "发送成功",
							Toast.LENGTH_LONG).show();
					ed_shoujian.setText("");
					ed_content.setText("");

				} else {
					//电话格式与短信字符不符合的条件下 Toast提醒用户
					if (isPhoneNumberValid(strAddress) == false) {
						if (iswithin70(strMessage) == false) {
							Toast.makeText(getApplicationContext(),
									"电话号码格式错误,短信内容超过七十个字,请检查",
									Toast.LENGTH_LONG).show();
						}
						else{
							Toast.makeText(getApplicationContext(), "电话号码格式错误", Toast.LENGTH_LONG).show();
						}
					}
					//短信字符数超过70
					else if(iswithin70(strMessage)==false){
						
						Toast.makeText(getApplicationContext(), "短信内容超过七十个,请删除部门内容", Toast.LENGTH_LONG).show();
					}
				}
			}
		});
	}

	protected static boolean iswithin70(String text) {
		// TODO Auto-generated method stub
		if(text.length()<=70){
			return true;
			
		}
		else{
			return false;
		}
		 
	}
//检查字符串是否为电话号码
	protected boolean isPhoneNumberValid(String phoneNumber) {
		boolean isValid=false;
		String expression="^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{5})$";
		String expression2="^\\(?(\\d{3})\\)?[- ]?(\\d{4})[- ]?(\\d{4})$";
		CharSequence inputStr=phoneNumber;
		Pattern pattern=Pattern.compile(expression);
		Matcher matcher=pattern.matcher(inputStr);
		Pattern pattern2=Pattern.compile(expression2);
		Matcher matcher2=pattern.matcher(inputStr);
		if(matcher.matches()||matcher2.matches()){
			isValid=true;
		}
		// TODO Auto-generated method stub
		return isValid;
	}
}

 

定义的XML

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/tv_shoujian"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="收件人" />

        <EditText
            android:id="@+id/ed_shoujian"
            android:layout_width="314dp"
            android:layout_height="wrap_content"
            android:hint="请输入收件人" />
    </LinearLayout>

    <EditText
        android:id="@+id/ed_content"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.35"
        android:hint="请输入内容" />

    <Button
        android:id="@+id/btn_send"
        android:layout_width="76dp"
        android:layout_height="wrap_content"
        android:text="发送" />

</LinearLayout>

 

发送短信的权限   要在mainfest.xml 里边定义

<uses-permission android:name="android.permission.SEND_SMS"/>
以上程序就完成了发送短信的功能    (发送短信的内容不能超过70个字符,然后电话号码不能是其他字符)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值