一个方法 smsManager.sendTextMessage(number, null, message, mSendPI, deliveredPI)
(android.telephony.SmsManager smsManager = android.telephony.SmsManager.getDefault()) sendTextMessage的参数:第一个是发送的号码 第二个设为null就可以(短信服务中心) 第三是发送内容 第四个参数 当短信发送成功或者失败时,这个mSendPI会被广播出去成功的结果代码是Activity.RESULT_OK,或者下面这些错误之一
RESULT_ERROR_GENERIC_FAILURE |
主要的方法:
private void sendSMS (){String SENT_SMS_ACTION = "sms_sent_action" ;String DELIVERED = "sms_delivered" ;String KEY_PHONENUM = "key_photo" ;Intent itSend = new Intent ( SENT_SMS_ACTION );itSend . putExtra ( KEY_PHONENUM , number );mSendPI = PendingIntent . getBroadcast ( getApplicationContext (),( int ) System . currentTimeMillis (), itSend , PendingIntent . FLAG_UPDATE_CURRENT );Intent itdelivere = new Intent ( SENT_SMS_ACTION );itdelivere . putExtra ( KEY_PHONENUM , number );deliveredPI = PendingIntent . getBroadcast ( getApplicationContext (), ( int ) System . currentTimeMillis (), itdelivere , PendingIntent . FLAG_UPDATE_CURRENT );smsManager.sendTextMessage(number, null, message, mSendPI, deliveredPI);registerReceiver ( new BroadcastReceiver (){@Overridepublic void onReceive ( Context context , Intent intent ) {switch ( getResultCode ()) {case Activity . RESULT_OK :succ ++;callBack . execute ( succ );break ;case SmsManager . RESULT_ERROR_GENERIC_FAILURE :break ;case SmsManager . RESULT_ERROR_NO_SERVICE :break ;case SmsManager . RESULT_ERROR_NULL_PDU :break ;case SmsManager . RESULT_ERROR_RADIO_OFF :break ;}}}, new IntentFilter ( SENT_SMS_ACTION ));registerReceiver ( new BroadcastReceiver (){@Overridepublic void onReceive ( Context context , Intent intent ){switch ( getResultCode ()) {case Activity . RESULT_OK :break ;case Activity . RESULT_CANCELED :break ;}}}, new IntentFilter ( DELIVERED )); }
定时操作可以在一个service里起一个线程:
Runnable runnable = new Runnable() {
public void run() {
while (true) {
if(N==Integer.parseInt(c)){ //N为发送的个数 c为设置要发送的个数
N = 0;
break;
}
N++;
smsManager.sendTextMessage(n, null, m, mSendPI, null/*deliveredPI*/);
try {
Thread.sleep(timeInterval*2000); //2s 发送一条, 当发送条数多时,且低于2s会有提示。
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
Thread thread = new Thread(runnable);
thread.start();