package com.example.sendmessage;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
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 MainActivity extends Activity {
private EditText et_phone, et_content;
private Button bt;
private Context context;
String SENT_SMS_ACTION = "SENT_SMS_ACTION";
String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_phone = (EditText) findViewById(R.id.et_phone);
et_content = (EditText) findViewById(R.id.et_content);
bt = (Button) findViewById(R.id.bt_send);
context = MainActivity.this;
// 注册通知
// IntentFilter filter = new IntentFilter();
// filter.addAction(SENT_SMS_ACTION);
// filter.addAction(DELIVERED_SMS_ACTION);
// registerReceiver(this.broadcastReceiver, filter);
// 获取短信管理器
// final SmsManager smsManager = SmsManager.getDefault();
// // PendingIntent sentIntent = PendingIntent.getBroadcast(
// // MainActivity.this, 0, new Intent(), 0);
// Intent sentIntent = new Intent(SENT_SMS_ACTION);
// final PendingIntent sentPI = PendingIntent.getBroadcast(context, 0,
// sentIntent, 0);
// // create the deilverIntent parameter
// Intent deliverIntent = new Intent(DELIVERED_SMS_ACTION);
// final PendingIntent deliverPI = PendingIntent.getBroadcast(context,
// 0,
// deliverIntent, 0);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
sendMes();
}
});
// context.registerReceiver(new BroadcastReceiver() {
// @Override
// public void onReceive(Context _context, Intent _intent) {
// switch (getResultCode()) {
// case Activity.RESULT_OK:
// Toast.makeText(context, "短信发送成功", Toast.LENGTH_SHORT)
// .show();
// break;
// case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
// Toast.makeText(context,
// SmsManager.RESULT_ERROR_GENERIC_FAILURE,
// Toast.LENGTH_SHORT).show();
// break;
// case SmsManager.RESULT_ERROR_RADIO_OFF:
// Toast.makeText(context, SmsManager.RESULT_ERROR_RADIO_OFF,
// Toast.LENGTH_SHORT).show();
// break;
// case SmsManager.RESULT_ERROR_NULL_PDU:
// Toast.makeText(context, SmsManager.RESULT_ERROR_NULL_PDU,
// Toast.LENGTH_SHORT).show();
// break;
// }
// }
// }, new IntentFilter(SENT_SMS_ACTION));
// context.registerReceiver(new BroadcastReceiver() {
// @Override
// public void onReceive(Context _context, Intent _intent) {
// Toast.makeText(context, "收信人已经成功接收", Toast.LENGTH_SHORT).show();
// }
// }, new IntentFilter(DELIVERED_SMS_ACTION));
}
private void sendMes() {
// 直接调用短信接口发短信
String mobile = et_phone.getText().toString();
String content = et_content.getText().toString();
// 判断输入是否为空
if (mobile == null || "".equals(mobile)) {
Toast.makeText(MainActivity.this, "发送号码不能为空!", Toast.LENGTH_SHORT)
.show();
return;
}
// if (!isPhoneNumberValid(mobile)) {
// Toast.makeText(MainActivity.this, "发送号码格式不正确!",
// Toast.LENGTH_SHORT).show();
// return;
// }
if (content == null || "".equals(content)) {
Toast.makeText(MainActivity.this, "发送内容不能为空!", Toast.LENGTH_SHORT)
.show();
return;
}
// 调起系统发短信功能
// Uri uri = Uri.parse("smsto:10010");
// Intent it = new Intent(Intent.ACTION_SENDTO, uri);
// it.putExtra("sms_body", "102");
// startActivity(it);
try {
// 获取短信管理器
final SmsManager smsManager = SmsManager.getDefault();
Intent sentIntent = new Intent(SENT_SMS_ACTION);
PendingIntent sentPI = PendingIntent.getBroadcast(context, 0,
sentIntent, 0);
// SmsManager smsManager = SmsManager.getDefault();
// PendingIntent sentIntent = PendingIntent.getBroadcast(
// MainActivity.this, 0, new Intent(), 0);
if (content.length() >= 70) {
// 短信字数大于70,自动分条
List<String> ms = smsManager.divideMessage(content);
for (String str : ms) {
// 短信发送
smsManager.sendTextMessage(mobile, null, str, sentPI, null);
}
} else {
smsManager.sendTextMessage(mobile, null, content, sentPI, null);
}
} catch (Exception e) {
e.printStackTrace();
}
// Toast.makeText(MainActivity.this, "发送成功!", Toast.LENGTH_LONG).show();
context.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context _context, Intent _intent) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(context, "短信发送成功", Toast.LENGTH_SHORT)
.show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
// Toast.makeText(context,
// SmsManager.RESULT_ERROR_GENERIC_FAILURE,
// Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
// Toast.makeText(context,
// SmsManager.RESULT_ERROR_RADIO_OFF,
// Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
// Toast.makeText(context, SmsManager.RESULT_ERROR_NULL_PDU,
// Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT_SMS_ACTION));
}
/* 检查字符串是否为电话号码的方法,并回传true or false的判断值 */
public static boolean isPhoneNumberValid(String phoneNumber) {
boolean isValid = false;
/*
* 可接受的电话格式有: * ^\\(? : 可以使用 "(" 作为开头 * (\\d{3}): 紧接着三个数字 * \\)? :
* 可以使用")"接续 * [- ]? : 在上述格式后可以使用具选择性的 "-". * (\\d{3}) : 再紧接着三个数字 * [-
* ]? : 可以使用具选择性的 "-" 接续. * (\\d{4})$: 以四个数字结束. * 可以比对下列数字格式: *
* (123)456-7890, 123-456-7890, 1234567890, (123)-456-7890
*/
String expression = "^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{4})$";
/*
* 可接受的电话格式有: * ^\\(? : 可以使用 "(" 作为开头 * (\\d{2}): 紧接着两个数字 * \\)? :
* 可以使用")"接续 * [- ]? : 在上述格式后可以使用具选择性的 "-". * (\\d{4}) : 再紧接着四个数字 * [-
* ]? : 可以使用具选择性的 "-" 接续. * (\\d{4})$: 以四个数字结束. * 可以比对下列数字格式: *
* (123)456-7890, 123-456-7890, 1234567890, (123)-456-7890
*/
String expression2 = "^\\(?(\\d{2})\\)?[- ]?(\\d{4})[- ]?(\\d{4})$";
CharSequence inputStr = phoneNumber;
/* 建立Pattern */Pattern pattern = Pattern.compile(expression);
/* 将Pattern 以参数传入Matcher作Regular expression */
Matcher matcher = pattern.matcher(inputStr);
/* 建立Pattern2 */Pattern pattern2 = Pattern.compile(expression2);
/* 将Pattern2 以参数传入Matcher2作Regular expression */
Matcher matcher2 = pattern2.matcher(inputStr);
if (matcher.matches() || matcher2.matches()) {
isValid = true;
}
return isValid;
}
}
<?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" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="电话号码" /> <!-- 电话号码输入 --> <EditText android:id="@+id/et_phone" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="内容" /> <!-- 短信内容编辑 --> <EditText android:id="@+id/et_content" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="left" android:minLines="3" /> <!-- 可3行显示 --> <!-- 设置左边输入 --> <Button android:id="@+id/bt_send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送" /> </LinearLayout>
manifest: <!-- 添加短信服务 --> <uses-permission android:name="android.permission.SEND_SMS" /> <uses-permission android:name="android.permission.READ_SMS" /> <uses-permission android:name="android.permission.INTERNET" />