• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

获取发送的短信

Android 来源:Hilder Vitor Lima Pereira 4次浏览

我正在制作一个在Android上发送短信的程序。 它工作正常发送,但我有一个litle问题知道什么信息被发送。 以下代码是代码的主要部分: 它从字符串resp中读取许多消息,等待一个随机时间并发送它们中的每一个。但是,我不知道这个SMS的方法onReceive正在确认。获取发送的短信

所以,这是我的问题:我怎么知道很多的短信发送了onReceive确认? 我试图寻找变量和方法两个参数上下文意图,但它并没有帮助我。

谢谢!

final PendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent("SMS_SENT"),0); 
registerReceiver(new BroadcastReceiver() { 

// executed when the sms is sended 
public void onReceive(Context context, Intent intent) { 
     switch(getResultCode()){ 
      case Activity.RESULT_OK: 
       Toast.makeText(getApplicationContext(), "SMS sended", 
         Toast.LENGTH_LONG).show(); 
      break; 
      default: 
       Toast.makeText(getApplicationContext(), "Error", 
         Toast.LENGTH_LONG).show(); 
      break; 
     } 
} 
}, new IntentFilter("SMS_SENT")); 

// resp is a String with a number and a body by line: <number><body>\n<number><body>\n... 
final Scanner scan = new Scanner(resp);   
String to; 
String body; 
SmsManager sms = SmsManager.getDefault(); 

while (scan.hasNext()){ 
     long r = new Random().nextInt(20000) + 1000; 
     synchronized(this){ 
      try { 
       this.wait(r); 
      } catch (InterruptedException e) { 
       Toast.makeText(getApplicationContext(), "ERROR on WAIT",Toast.LENGTH_LONG).show(); 
      } 
     } 
     to = scan.next(); 
     body = scan.nextLine(); 
     Toast.makeText(getApplicationContext(), "Sending to " + to, 
       Toast.LENGTH_LONG).show(); 

     sms.sendTextMessage(to, null, body, pi, null); 
} 
scan.close(); 

===========解决方案如下:

编辑:稍微修改了代码,能够保存广播接收器用于注销

的目的,你可以做到这一点无论是通过增加“额外”的Intent您传递给PendingIntent.getBroadcast()或在“ACTION”中对消息ID进行编码,您在Intent中输入的值为getBroadcast()

由于在PendingIntent中处理额外事件的方式,使用“额外”更为复杂。下面是我在ACTION中对消息ID进行编码的一个示例:

long messageID = ...; // This is the message ID (some unique value so that you know which message was sent) 
String actionString = "SMS_SENT_" + messageID; 

final PendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent(actionString),0); 
BroadcastReceiver myBroadcastReceiver = new BroadcastReceiver() { 

    // executed when the sms is sended 
    public void onReceive(Context context, Intent intent) { 
     // Extract message ID from action 
     String action = intent.getAction(); 
     long id = -1; // Message ID 
     if (action != null) { 
      id = Long.parseLong(action.substring(9)); 
      // id is now your message ID 
     } 
     switch(getResultCode()){ 
      case Activity.RESULT_OK: 
       Toast.makeText(getApplicationContext(), "SMS sended", 
        Toast.LENGTH_LONG).show(); 
      break; 
      default: 
       Toast.makeText(getApplicationContext(), "Error", 
        Toast.LENGTH_LONG).show(); 
      break; 
     } 
    } 
}; 
registerReceiver(myBroadcastReceiver, new IntentFilter(actionString)); 
// Here you can save myBroadcastReceiver somewhere (in an ArrayList maybe?) 
// so that you can unregister later 

版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)