2013-12-16 58 views
1

我正在制作一个在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(); 

回答

1

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

的目的,你可以做到这一点无论是通过增加“额外”的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 
+1

看起来很酷。我已经有了每个SMS的唯一ID,导致所有消息都在数据库中...所以,我需要为每个SMS发送一个PendingIntent?我会尝试并回来给你一些反馈。谢谢。 –

+0

有什么奇怪的...我需要为每个SMS注册一个_onReceive_?它工作不正常。第一个onReceive被执行,但应用程序“死”之前执行其他人...一些想法?再次感谢你。 –

+0

好吧,我认为这是一个“等待”的问题... –