package app.test;
import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;
import android.util.Log;
class MySMSMonitor extends BroadcastReceiver {
private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
@Override
public void onReceive(Context context, Intent intent)
{
if(intent!=null && intent.getAction()!=null &&
ACTION.compareToIgnoreCase(intent.getAction())==0)
{
Object[]pduArray = (Object[]) intent.getExtras().get("pdus");
SmsMessage[] messages = new SmsMessage[pduArray.length];
for (int i = 0; i messages[i] = SmsMessage.createFromPdu((byte[])pduArray [i]);
Log.d("MySMSMonitor", "From: " + messages[i].getOriginatingAddress());
Log.d("MySMSMonitor", "Msg: " + messages[i].getMessageBody());
}
Log.d("MySMSMonitor","SMS Message Received.");
}
}
}
public class Test extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void doSend(View view) {
EditText addrTxt =
(EditText)findViewById(R.id.addrEditText);
EditText msgTxt =
(EditText)findViewById(R.id.msgEditText);
try {
sendSmsMessage(
addrTxt.getText().toString(),
msgTxt.getText().toString());
Toast.makeText(this, "SMS Sent",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(this, "Failed to send SMS",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
}
private void sendSmsMessage(String address,String message)throws Exception
{
SmsManager smsMgr = SmsManager.getDefault();
smsMgr.sendTextMessage(address, null, message, null, null);
}
}
//main.xml
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content">
android:layout_height="wrap_content" android:text="Destination Address:" />
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:phoneNumber="true" android:text="9045551212" />
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content">
android:layout_height="wrap_content" android:text="Text Message:" />
android:layout_height="wrap_content" android:text="hello sms" />
android:layout_height="wrap_content" android:text="Send Text Message"
android:onClick="doSend" />