Pertama, buatlah project Android, misalnya dengan nama SendSMS
SendSMSActivity.java
package com.amijaya.SendSMSActivity;
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.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class SendSMSActivity extends Activity
{
Button btnKirimSMS;
EditText txtNoTelp;
EditText txtPesan;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnKirimSMS = (Button) findViewById(R.id.btn KirimSMS);
txtNoTelp = (EditText) findViewById(R.id.txt NoTelp);
txtPesan = (EditText) findViewById(R.id.txtPesan);
btnKirimSMS.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
String noTelp = txtNoTelp.getText().toString();
String pesan = txtPesan.getText().toString();
if ( noTelp.length()>0 && pesan.length()>0)
{
kirimSMS(noTelp, pesan);
}
}
else
Toast.makeText(getBaseContext(),
"Silakan Masukkan No Telp dan Pesan.",
Toast.LENGTH_SHORT).show();
}
});
}
private void KirimSMS(String noTelp, String pesan)
{
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---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS terkirim",
Toast.LENGTH_SHORT).show();
break;
case android.telephony.SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "error",
Toast.LENGTH_SHORT).show();
break;
case android.telephony.SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "Tidak ada sinyal",
Toast.LENGTH_SHORT).show();
break;
case android.telephony.SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case android.telephony.SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "GSM mati",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
//---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));
android.telephony.SmsManager sms = android.telephony.SmsManager.getDefault();
sms.sendTextMessage(noTelp, null, pesan, sentPI, deliveredPI);
}
}
Buat kelas baru bernama PenerimaSMS.java yang berfungsi sebagai Broadcast yang mendengarkan jika ada SMS masuk (delivered) :
package com.amijaya.SendSMS;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.widget.Toast;
public class PenerimaSMS extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS dari " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
}
}
Buka layout/main.xml untuk membuat design tampilannya :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter the phone number of recipient"
/>
<EditText
android:id="@+id/txtNoTelp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Pesan"
/>
<EditText
android:id="@+id/txtPesan"
android:layout_width="fill_parent"
android:layout_height="150px"
android:gravity="top"
/>
<Button
android:id="@+id/btnKirimSMS"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Kirim SMS"
/>
</LinearLayout>
Buka AndroidManifest.xml, kemudian tambahkan baris untuk mendeklarasikan PenerimaSMS dan Permisiion untuk SEND_SMS dan RECEIVE_SMS :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.amijaya.SendSMS"
android:versionCode="1"
android:versionName="1.0.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".SendSMSActivity "
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".PenerimaSMS">
<intent-filter>
<action android:name=
"android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.SEND_SMS">
</uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS">
</uses-permission>
</manifest>