zoukankan      html  css  js  c++  java
  • Sending SMS on Android and tracking it

    Lets look at how to send an SMS. It's pretty simple though. A few lines of code and your SMS is gone. To make it more convenient and meaningful, we should also be able to track and let the user know when the SMS is sent and when it is delivered. For a start, I won't be looking into how to trap the error messages here and log or show it to the user. May be, when I get some more time, I will update this post. For now, lets track our SMS.

    Intent sentIntent = new Intent(INTENT_ACTION_SENT);
    PendingIntent pendingSentIntent = PendingIntent.getBroadcast(this,
    REQUEST_CODE_ACTION_SENT, sentIntent,
    PendingIntent.FLAG_UPDATE_CURRENT);

    Intent deliveryIntent = new Intent(INTENT_ACTION_DELIVERY);
    PendingIntent pendingDeliveryIntent = PendingIntent.getBroadcast(this,
    REQUEST_CODE_ACTION_DELIVERY, deliveryIntent,
    PendingIntent.FLAG_UPDATE_CURRENT);

    SmsManager smsManager = SmsManager.getDefault();

    // Second parameter is the service center number. Use null if you want
    // to use the default number
    smsManager.sendTextMessage(number, null, message, pendingSentIntent,
    pendingDeliveryIntent);

    In the above code snipped, you can see that we are passing 2 pending intents to the SMSManager, one of which will be fired when the SMS is sent, and the other, when the SMS is delivered. It would also let you know the error type if the sending or delivery fails, so that you can take action for the errors. INTENT_ACTION_SENT and INTENT_ACTION_DELIVERY are string constants, which are just some random actions required to setup of the PendingIntents and receive them back.

    Setting up the SMS is super easy. How do we track or listen to the updates, which happen through the PendingIntents? Well, those pending intents could be for starting an Activity, a Service or sending out a Broadcast. As you can see, here, I have used a Broadcast, to keep it simple. So, in our activity, we would need to register BroadcastReceivers for the same actions.

    IntentFilter filter = new IntentFilter(INTENT_ACTION_SENT);
    filter.addAction(INTENT_ACTION_DELIVERY);

    registerReceiver(smsSentDeliveredReceiver, filter);

    Now, the onReceive() method will be fired, when those events happen, and thus you can notify the user about when the message is sent and delivered.

    String action = intent.getAction();
    Log.i(TAG, "Received: " + action);

    if (action.equals(INTENT_ACTION_SENT)) {
      Log.i(TAG, "Message: Sent");
      Toast.makeText(this, "Message sent", Toast.LENGTH_LONG).show();
    } else if (action.equals(INTENT_ACTION_DELIVERY)) {
      Log.i(TAG, "Message: Delivered");
      Toast.makeText(this, "Message delivered", Toast.LENGTH_LONG).show();
    }




  • 相关阅读:
    Velocity 创建命令缓存
    关于RESTful Web Services和RESTful设计的一些文章
    [转]Restful Web Services浅析
    Silverlight点滴(四)Silverlight访问Web Service报"System.Security.SecurityException: 安全性错误"的处理
    WebDevHelper RESTful服务和Ajax开发时的利器
    [原]读Google Data API源代码一:从创建一个日历(Calendar)开始
    [转]使用 WCF 和 .NET Framework 3.5 进行 HTTP 编程
    [原]Silverlight3 支持所有HTTP方法
    [原]RESTful Web Service之以HTTP PUT方式调用WCF服务
    [原]Google API学习2:Google API稍深入一步
  • 原文地址:https://www.cnblogs.com/xpxpxp2046/p/2349361.html
Copyright © 2011-2022 走看看