http://ukitech.blogspot.com/2014/11/android-calling-activity-from.html
We will learn how to SENT a SMS programmatically.
Step: Improve UI
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:text="If SMS starts with.. "
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/sms_search_text"
android:layout_marginLeft="32dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="CIT299?"/>
<TextView
android:text="Respond with the SMS message.."
android:layout_marginTop="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/sms_respond_text"
android:layout_marginLeft="32dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="finish this app, study hard, prepare the final"/>
<TextView
android:text="Received SMS.."
android:layout_marginTop="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="100dp">
<TextView
android:id="@+id/sms_received"
android:layout_marginLeft="32dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:hint="1\n2\n3\n4\n5\n6\n7\n8\n9\n"/>
</ScrollView>
</LinearLayout>
Step: new class SmsSender.java
package com.chicagoandroid.android.app.sms;
import android.app.PendingIntent;
import android.content.Context;
import android.telephony.PhoneNumberUtils;
import android.telephony.SmsManager;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
/**
* Created by uki on 11/22/14.
*/
public class SmsSender {
private static final String TAG = SmsSender.class.getSimpleName();
Context appContext;
public SmsSender(Context context) {
appContext = context;
}
public void send(String destinationAddress, String text) {
if (!PhoneNumberUtils.isWellFormedSmsAddress(destinationAddress)) {
Log.e(TAG, "SMS number is malformed");
return;
}
SmsManager smsManager = SmsManager.getDefault();
String scAddress = null; //getMyPhoneNumber();
PendingIntent sentIntent = null;
PendingIntent deliveryIntent = null; // broadcast when delivered
try {
smsManager.sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent);
Log.w(TAG, "message sent: " + text + " to " + destinationAddress);
}
catch (Exception e) {
Log.e(TAG, "SMS not sent: " + e.getMessage());
//TODO handle
}
}
Step: Send message from SmsParser
public void processReceivedSms(String smsOriginatingAddress, String smsDisplayMessage) {
final String tag = TAG + ".processReceivedSms";
Log.i(tag, "SMS from " + smsOriginatingAddress);
Log.i(tag, "SMS body " + smsDisplayMessage);
mainActivity.appendSmsToUI("SMS from " + smsOriginatingAddress + "\n" + smsDisplayMessage);
String keyword = mainActivity.getSmsTextSearch();
if (doesSmsStartWith(smsDisplayMessage, keyword)) {
Log.i(tag, "SMS does start with " + keyword);
String responseText = mainActivity.getSmsRespondSearch();
Log.i(tag, "Attempting to respond: " + responseText);
SmsSender sender = new SmsSender(appContext);
sender.send(smsOriginatingAddress, responseText);
}
else
Log.e(TAG + ".onReceive", "SMS does not start with " + keyword);
}
Step: MainActivity - full listing
No comments:
Post a Comment