Sunday, March 10, 2013

SMSplugin Phonegap android

Follows these steps:

1. Include the smsplugin.js to your project within asset/www/ folder and include a reference to it in your html files.
Copy and paste below the js code to your smsplugin.js file.
var SmsPlugin = function() {};
var sendSMS = {
send : function(phone, message, successCallback, failureCallback) {
return cordova.exec(successCallback, failureCallback, ‘SmsPlugin’,
“SendSMS”, [ phone, message ]);
}
};
if (!window.Cordova) {
window.Cordova = cordova;
};
if (!window.plugins)
window.plugins = {};
window.plugins.SmsPlugin = new SmsPlugin();
2. Add the sms java file to your scr to your project’s hierarchy.
   Copy and paste below the code to your sms java file.
import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import org.apache.cordova.api.PluginResult.Status;
import org.json.JSONArray;
import org.json.JSONException;
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.telephony.SmsManager;
import android.telephony.TelephonyManager;
import android.widget.Toast;

public class SmsPlugin extends Plugin {
public final String ACTION_SEND_SMS = “SendSMS”;
public int mToastTime = 300000;
public static PluginResult result;
@Override
public PluginResult execute(String action, JSONArray arg1, String callbackId) {
result = new PluginResult(Status.INVALID_ACTION);
String phoneNumber = null;
String message = null;
try {
phoneNumber = arg1.getString(0);
message = arg1.getString(1);
} catch (JSONException e) {
e.printStackTrace();
}
if (phoneNumber.length() > 0 && message.length() > 0) {
if (action.equals(ACTION_SEND_SMS)) {
TelephonyManager telMgr = (TelephonyManager) cordova.getActivity().getSystemService(Context.TELEPHONY_SERVICE);
int simState = telMgr.getSimState();
switch (simState) {
case TelephonyManager.SIM_STATE_ABSENT:
Toast.makeText(cordova.getActivity(), “No Sim Card found”, mToastTime).show();
break;
case TelephonyManager.SIM_STATE_NETWORK_LOCKED:
Toast.makeText(cordova.getActivity(), “SIM_STATE_NETWORK_LOCKED”, mToastTime).show();
break;
case TelephonyManager.SIM_STATE_PIN_REQUIRED:
Toast.makeText(cordova.getActivity(), “SIM_STATE_PIN_REQUIRED”, mToastTime).show();
break;
case TelephonyManager.SIM_STATE_PUK_REQUIRED:
Toast.makeText(cordova.getActivity(), “SIM_STATE_PUK_REQUIRED”, mToastTime).show();
break;
case TelephonyManager.SIM_STATE_READY:
sendSMS(phoneNumber, message); // method to send message
break;
case TelephonyManager.SIM_STATE_UNKNOWN:
Toast.makeText(cordova.getActivity(), “SIM_STATE_UNKNOWN”, mToastTime).show();
break;
}
}
} else
Toast.makeText(cordova.getActivity(), “Please enter both phone number and message.”, mToastTime).show();
return stopTimer(result);
}

private void sendSMS(String phoneNumber, String message) {
String SENT = “SMS_SENT”;
String DELIVERED = “SMS_DELIVERED”;
PendingIntent sentPI = PendingIntent.getBroadcast(cordova.getActivity(), 0, new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(cordova.getActivity(), 0, new Intent(DELIVERED), 0);
// —when the SMS has been sent—final String string =
cordova.getActivity().registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(cordova.getActivity(), “SMS sent”, mToastTime).show();
result = new PluginResult(Status.OK);
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(cordova.getActivity(), “Generic failure”, mToastTime).show();
result = new PluginResult(Status.ERROR);
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(cordova.getActivity(), “No service”, mToastTime).show();
result = new PluginResult(Status.ERROR);
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(cordova.getActivity(), “Null PDU”, mToastTime).show();
result = new PluginResult(Status.ERROR);
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(cordova.getActivity(), “Radio off”, mToastTime).show();
result = new PluginResult(Status.ERROR);
break;
}
}
}, new IntentFilter(SENT));
// —when the SMS has been delivered—
cordova.getActivity().registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(cordova.getActivity(), “SMS delivered”, mToastTime).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(cordova.getActivity(), “SMS not delivered”, mToastTime).show();
break;
}
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
public static PluginResult stopTimer(PluginResult st) {
Thread mSplashThread = new Thread() {
public void run() {
try {
synchronized (this) {
wait(5000);
}
} catch (InterruptedException ex) {
}
}
};
mSplashThread.start();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return result;
}
}
3. Add the refrence plugin in you res/plugin.xml file.
<plugin name=”SmsPlugin”  value=”com.example.phonegapapp.SmsPlugin” />
4. Add the sms permission in manifest file.
<uses-permission android:name=”android.permission.SEND_SMS” /> 
5. Call the following function in java script file to send the sms.
sendSMS.send(‘Mobile Number’, ‘Text’, ‘Call back function on complete’);

2 comments:

  1. It would be great if this plugin could be submitted to https://build.phonegap.com/plugins
    What do you think?

    ReplyDelete
    Replies
    1. That's a great Idea...I'll submit it on there ... thanks for your suggestion.

      Delete