Created
January 22, 2012 02:30
-
-
Save aztack/1655135 to your computer and use it in GitHub Desktop.
send sms in android 2.2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SendSMS extends Activity { | |
private void setStatus(String text) | |
{ | |
TextView status = (TextView)findViewById(R.id.status); | |
status.setText(text); | |
} | |
private void sendSMS(String phoneNumber, String message) | |
{ | |
String SENT = "SMS_SENT"; | |
String DELIVERED = "SMS_DELIVERED"; | |
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, | |
new Intent(SENT), 0); | |
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, | |
new Intent(DELIVERED), 0); | |
//---when the SMS has been sent--- | |
BroadcastReceiver br = new BroadcastReceiver(){ | |
@Override | |
public void onReceive(Context arg0, Intent arg1) { | |
switch (getResultCode()) | |
{ | |
case Activity.RESULT_OK: | |
setStatus("短信已发送"); | |
break; | |
case SmsManager.RESULT_ERROR_GENERIC_FAILURE: | |
setStatus("发送失败"); | |
break; | |
case SmsManager.RESULT_ERROR_NO_SERVICE: | |
setStatus("不在服务区"); | |
break; | |
case SmsManager.RESULT_ERROR_NULL_PDU: | |
setStatus("Null PDU"); | |
break; | |
case SmsManager.RESULT_ERROR_RADIO_OFF: | |
setStatus("Radio off"); | |
break; | |
} | |
unregisterReceiver(this); | |
} | |
}; | |
registerReceiver(br,new IntentFilter(SENT)); | |
//---when the SMS has been delivered--- | |
BroadcastReceiver br2 = new BroadcastReceiver(){ | |
@Override | |
public void onReceive(Context arg0, Intent arg1) { | |
switch (getResultCode()) | |
{ | |
case Activity.RESULT_OK: | |
setStatus("对方已收到短信"); | |
break; | |
case Activity.RESULT_CANCELED: | |
setStatus("SMS not delivered"); | |
} | |
unregisterReceiver(this); | |
} | |
}; | |
registerReceiver(br2,new IntentFilter(DELIVERED)); | |
SmsManager sms = SmsManager.getDefault(); | |
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment