android - 获取发送的短信

标签 android sms

我正在制作一个在 Android 上发送 SMS 消息的程序。 发送工作正常,但我不知道发送了哪条消息。

下面的代码是代码的主要部分: 它从字符串 resp 中读取许多消息,随机等待一段时间并发送每条消息。但是,我不知道 onReceive 方法确认的是哪条短信。

所以,这是我的问题:我如何知道 onReceive 发送的哪条 SMS 正在确认?

我试图寻找contextintent 这两个参数的变量和方法,但没有帮助。

 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();
 

最佳答案

编辑:稍微修改代码以便能够保存 BroadcastReceiver 以用于注销

您可以通过向传递给 PendingIntent.getBroadcast()Intent 添加“extras”或通过在“ACTION”中编码消息 ID 来实现此目的您放入传递给 getBroadcast()Intent

由于在 PendingIntent 中处理 extras 的方式,使用“extras”更加复杂。这是我在 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

关于android - 获取发送的短信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20616731/

相关文章:

java - 如何在 MSWord 中使用 Intent

objective-c - iOS 6.0 中的 MessageUI 问题

java - 如何在 StringBuilder 中将项目与 JsonObject 分开?

android - Lateinit 属性 mScrollView 尚未初始化

java - Java 7 中 ComputeIfAbsent 的等价物

android - 在 Android 中通过 getApplicationContext() 使用静态方法?

android - 启动应用程序接收来自特定号码的短信

android - 如何让 QVideoProbe 工作?

java - 如何用Java程序读取大量短信?

android - 在Android中将短信转换为语音(音频)的代码