Mengirim dan Menerima SMS dengan Android

Android menyertakan kelas SMS Manager yang dapat digunakan untuk mengirim dan menerima SMS.

 

  1: package kalbis.mobile.communication;
  2:
  3: import android.os.Bundle;
  4: import android.app.Activity;
  5: import android.app.PendingIntent;
  6: import android.content.BroadcastReceiver;
  7: import android.content.Context;
  8: import android.content.Intent;
  9: import android.content.IntentFilter;
 10: import android.telephony.gsm.SmsManager;
 11: import android.view.Menu;
 12: import android.view.View;
 13: import android.view.View.OnClickListener;
 14: import android.widget.Button;
 15: import android.widget.EditText;
 16: import android.widget.TextView;
 17: import android.widget.Toast;
 18:
 19: public class MainActivity extends Activity {
 20:
 21: 	Button btnSendBySMSManager;
 22: 	Button btnSendByIntent;
 23: 	EditText edtPhoneNumber;
 24: 	EditText edtSendMessage;
 25: 	TextView txtReceivedMessage;
 26:
 27: 	IntentFilter intentFilter;
 28:
 29: 	private BroadcastReceiver intentReceiver = new BroadcastReceiver() {
 30: 		@Override
 31: 		public void onReceive(Context context, Intent intent) {
 32: 			// ---display the SMS received in the TextView---
 33: 			txtReceivedMessage = (TextView) findViewById(R.id.textView3);
 34: 			txtReceivedMessage.setText(intent.getExtras().getString("SMS"));
 35: 		}
 36: 	};
 37:
 38: 	@Override
 39: 	protected void onCreate(Bundle savedInstanceState) {
 40: 		super.onCreate(savedInstanceState);
 41: 		setContentView(R.layout.activity_main);
 42:
 43: 		// ---intent to filter for SMS messages received---
 44: 		intentFilter = new IntentFilter();
 45: 		intentFilter.addAction("SMS_RECEIVED_ACTION");
 46:
 47: 		// ---register the receiver---
 48: 		 registerReceiver(intentReceiver, intentFilter);
 49:
 50: 		btnSendBySMSManager = (Button) findViewById(R.id.button1);
 51: 		btnSendByIntent = (Button) findViewById(R.id.button2);
 52: 		edtPhoneNumber = (EditText) findViewById(R.id.editText1);
 53: 		edtSendMessage = (EditText) findViewById(R.id.editText2);
 54:
 55: 		btnSendBySMSManager.setOnClickListener(new OnClickListener() {
 56: 			@Override
 57: 			public void onClick(View arg0) {
 58: 				String phoneNumber = edtPhoneNumber.getText().toString();
 59: 				String message = edtSendMessage.getText().toString();
 60: 				sendSMS(phoneNumber, message);
 61: 			}
 62: 		});
 63:
 64: 		btnSendByIntent.setOnClickListener(new OnClickListener() {
 65:
 66: 			@Override
 67: 			public void onClick(View v) {
 68: 				Intent i = new Intent(android.content.Intent.ACTION_VIEW);
 69: 				i.putExtra("address", edtPhoneNumber.getText().toString());
 70: 				i.putExtra("sms_body", edtSendMessage.getText().toString());
 71: 				i.setType("vnd.android-dir/mms-sms");
 72: 				startActivity(i);
 73: 			}
 74: 		});
 75: 	}
 76:
 77: 	@Override
 78: 	protected void onResume() {
 79: 		// ---register the receiver---
 80: //		registerReceiver(intentReceiver, intentFilter);
 81: 		super.onResume();
 82: 	}
 83:
 84: 	@Override
 85: 	protected void onPause() {
 86: 		// ---unregister the receiver---
 87: 		// unregisterReceiver(intentReceiver);
 88: 		super.onPause();
 89: 	}
 90:
 91: 	@Override
 92: 	protected void onDestroy() {
 93: 		// ---unregister the receiver---
 94: 		unregisterReceiver(intentReceiver);
 95: 		super.onDestroy();
 96: 	}
 97:
 98: 	@SuppressWarnings("deprecation")
 99: 	// ---sends an SMS message to another device---
100: 	private void sendSMS(String phoneNumber, String message) {
101: 		String SENT = "SMS_SENT";
102: 		String DELIVERED = "SMS_DELIVERED";
103:
104: 		PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
105: 				SENT), 0);
106:
107: 		PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
108: 				new Intent(DELIVERED), 0);
109:
110: 		// ---when the SMS has been sent---
111: 		registerReceiver(new BroadcastReceiver() {
112: 			@Override
113: 			public void onReceive(Context arg0, Intent arg1) {
114: 				switch (getResultCode()) {
115: 				case Activity.RESULT_OK:
116: 					Toast.makeText(getBaseContext(), "SMS sent",
117: 							Toast.LENGTH_SHORT).show();
118: 					break;
119: 				case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
120: 					Toast.makeText(getBaseContext(), "Generic failure",
121: 							Toast.LENGTH_SHORT).show();
122: 					break;
123: 				case SmsManager.RESULT_ERROR_NO_SERVICE:
124: 					Toast.makeText(getBaseContext(), "No service",
125: 							Toast.LENGTH_SHORT).show();
126: 					break;
127: 				case SmsManager.RESULT_ERROR_NULL_PDU:
128: 					Toast.makeText(getBaseContext(), "Null PDU",
129: 							Toast.LENGTH_SHORT).show();
130: 					break;
131: 				case SmsManager.RESULT_ERROR_RADIO_OFF:
132: 					Toast.makeText(getBaseContext(), "Radio off",
133: 							Toast.LENGTH_SHORT).show();
134: 					break;
135: 				}
136: 			}
137: 		}, new IntentFilter(SENT));
138:
139: 		// ---when the SMS has been delivered---
140: 		registerReceiver(new BroadcastReceiver() {
141: 			@Override
142: 			public void onReceive(Context arg0, Intent arg1) {
143: 				switch (getResultCode()) {
144: 				case Activity.RESULT_OK:
145: 					Toast.makeText(getBaseContext(), "SMS delivered",
146: 							Toast.LENGTH_SHORT).show();
147: 					break;
148: 				case Activity.RESULT_CANCELED:
149: 					Toast.makeText(getBaseContext(), "SMS not delivered",
150: 							Toast.LENGTH_SHORT).show();
151: 					break;
152: 				}
153: 			}
154: 		}, new IntentFilter(DELIVERED));
155:
156: 		SmsManager sms = SmsManager.getDefault();
157: 		sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
158: 	}
159:
160: 	@Override
161: 	public boolean onCreateOptionsMenu(Menu menu) {
162: 		// Inflate the menu; this adds items to the action bar if it is present.
163: 		getMenuInflater().inflate(R.menu.activity_main, menu);
164: 		return true;
165: 	}
166:
167: }
168:


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s