Code snippet:
public void sendSmsMessage(String phoneNumber,String message){
SmsManager smsManager = SmsManager.getDefault();
ArrayList<String> messageList = smsManager.divideMessage(message);
PendingIntent mPI = PendingIntent.getBroadcast(MainEntryActivity.this, 0, new Intent("SMS_SENT"), PendingIntent.FLAG_UPDATE_CURRENT);
ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
for(int i=0;i<messageList.size();i++){
sentIntents.add(mPI);
}
registerReceiver(smsSentReceiver, new IntentFilter("SMS_SENT"));
smsManager.sendMultipartTextMessage (phoneNumber, null, messageList, sentIntents, null);
}
private BroadcastReceiver smsSentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String result = "";
switch(getResultCode()) {
case Activity.RESULT_OK:
result = "Transmission successful";
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
result = "Transmission failed";
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
result = "Radio off";
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
result = "No PDU defined";
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
result = "No service";
break;
}
Log.d(TAG, "send sms result: " + result);
}
};
@Override
protected void onDestroy() {
try{
unregisterReceiver(smsSentReceiver);
} catch(IllegalArgumentException iae){
Log.d(TAG,"Receiver not registered");
iae.printStackTrace();
}
}
沒有留言:
張貼留言