I was working with sending scheduled SMS to the user's phone for my latest app and I found this code to do it after hours of using Google. However, I have slightly modified the code to fit my needs. This code emulate almost perfectly an actual sms received by the phone but remember that every phone does this differently. I'm just taking the assumption that most users still use stock messaging app.
First, create the interface.
main.xml
» Click to show Code - click again to hide... «
AndroidAlarmSMSActivity.java
» Click to show Code - click again to hide... «
SmsAlarmService.java
» Click to show Code - click again to hide... «
package com.exercise.AndroidAlarmSMS;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.telephony.gsm.SmsManager;
import android.widget.Toast;
public class SmsAlarmService extends Service {
private String smsNumberToSend, smsTextToSend;
@Override
public void onCreate() {
Toast.makeText(this, "SmsAlarmService.onCreate()", Toast.LENGTH_LONG)
.show();
}
@Override
public IBinder onBind(Intent arg0) {
Toast.makeText(this, "SmsAlarmService.onBind()", Toast.LENGTH_LONG)
.show();
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "SmsAlarmService.onDestroy()", Toast.LENGTH_LONG)
.show();
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Bundle bundle = intent.getExtras();
smsNumberToSend = (String) bundle.getCharSequence("extraSmsNumber");
smsTextToSend = (String) bundle.getCharSequence("extraSmsText");
Toast.makeText(this, "SmsAlarmService.onStart()", Toast.LENGTH_LONG)
.show();
Toast.makeText(
this,
"SmsAlarmService.onStart() with \n" + "smsNumberToSend = "
+ smsNumberToSend + "\n" + "smsTextToSend = "
+ smsTextToSend, Toast.LENGTH_LONG).show();
// This will write a message in the user phone sms inbox. Next, a
// notification will be made to emulate real sms received//
fakeSms();
// Use this to send real sms to the specified number (Warning: it cost
// money to even send to your own number)
//realSms();
makeNotification();
}
/**
* This will write a message in the user phone sms inbox.
*
* @param smsNumberToSend
* @param smsTextToSend
*/
private void fakeSms() {
ContentValues values = new ContentValues();
values.put("address", smsNumberToSend);
values.put("body", smsTextToSend);
getContentResolver().insert(Uri.parse("content://sms/inbox"), values);
}
/**
* send real sms to the specified number (even the phone the user is using)
*
* @param smsNumberToSend
* @param smsTextToSend
*/
private void realSms() {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(smsNumberToSend, null, smsTextToSend, null,
null);
}
@Override
public boolean onUnbind(Intent intent) {
Toast.makeText(this, "SmsAlarmService.onUnbind()", Toast.LENGTH_LONG)
.show();
return super.onUnbind(intent);
}
/**
* Send push notification with default ringtone, vibration and lights set by
* the user.
*
* @param from
* @param message
*/
private void makeNotification() {
// calculating how many unread sms in inbox
Uri SMS_INBOX = Uri.parse("content://sms/inbox");
Cursor c = getContentResolver().query(SMS_INBOX, null, "read = 0",
null, null);
int unreadMessagesCount = c.getCount();
c.deactivate();
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
Notification notification = new Notification(
android.R.drawable.ic_dialog_email, smsNumberToSend + ": "
+ smsTextToSend, System.currentTimeMillis());
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Context context = getApplicationContext();
CharSequence contentTitle = "New messages";
CharSequence contentText = unreadMessagesCount + " unread messages.";
Intent notificationIntent = new Intent(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_DEFAULT);
notificationIntent.setType("vnd.android-dir/mms-sms");
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText,
contentIntent);
int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);
}
}
Modify AndroidManifest.xml to add <service android:name=".MyAlarmService" />
Permisssions needed is VIBRATE, WRITE_SMS, READ_SMS, SEND_SMS.
» Click to show Code - click again to hide... «
Ten seconds later, the message will be written to the phone inbox. The push notification is made. Pressing the notification will go to the default messaging app.