Wednesday 23 July 2014

HB Blog 5: How to send sms programmatically using phonegap and android?

The mobile application development landscape is filled with many ways to build a mobile app.
Among the most popular are:

    Native iOS,
    Native Android,
    PhoneGap,
    PhoneGap Android Cordova Library,
    Appcelerator Titanium.

1)PhoneGap Android Cordova Library:-

 Refer the below link for starting with phonegap:-

    http://docs.phonegap.com/en/2.0.0/guide_getting-started_android_index.md.html

Refer the below link for complete sample code:-
Download Sample Code
Download Apk File



 Few important snippets for sms:-

   MainActivity.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
public class MainActivity extends DroidGap
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        // Set by <content src="index.html" /> in config.xml
        super.loadUrl(Config.getStartUrl());
        //super.loadUrl("file:///android_asset/www/index.html")
    }
}
  
  smsplugin.js
1
2
3
4
5
6
7
var SmsPlugin = function () {};
SmsPlugin.prototype.send = function (phone, message, method, successCallback, failureCallback) {   
    return PhoneGap.exec(successCallback, failureCallback, 'SmsPlugin', "SendSMS", [phone, message, method]);
};
PhoneGap.addConstructor(function() {
    PhoneGap.addPlugin("sms", new SmsPlugin());
});
  
  index.html
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<script type="text/javascript">
         $(document).ready(function() {
             //leave empty for sending sms using default intent
             $("#btnDefaultSMS").click(function(){
                 var number = $("#numberTxt").val();
                 var message = $("#messageTxt").val();
                SmsPlugin.prototype.send(number, message, '',
                    function () {
                       alert('Message sent successfully'); 
                    },
                    function (e) {
                        alert('Message Failed:' + e);
                    }
                );              
             });
         });
      </script>
2)Native Android:-

 Refer the below link for starting with android:-

   https://developer.android.com/training/index.html

Refer the below link for complete sample code:-
Download Sample Code
Download Apk File



 Few important snippets for sms:-

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
public void sendSMS(String phoneNumber,String message) {
        SmsManager smsManager = SmsManager.getDefault();
         String SENT = "SMS_SENT";
         String DELIVERED = "SMS_DELIVERED";
            SmsManager sms = SmsManager.getDefault();
            ArrayList<String> parts = sms.divideMessage(message);
            int messageCount = parts.size();        
     Log.i("Message Count", "Message Count: " + messageCount);    
         ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>();
            ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>(); 
            PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
            PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);
            for (int j = 0; j < messageCount; j++) {
                sentIntents.add(sentPI);
                deliveryIntents.add(deliveredPI);
            }
            // ---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 sent",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(getBaseContext(), "Generic failure",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(getBaseContext(), "No service",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(getBaseContext(), "Null PDU",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(getBaseContext(), "Radio off",
                                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));         
            smsManager.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
           /* sms.sendMultipartTextMessage(phoneNumber, null, parts, sentIntents, deliveryIntents); */
    }

No comments:

Post a Comment