区分两个独立 SMS 的交付报告 [英] Differentiating delivery reports of two separate SMS's

查看:40
本文介绍了区分两个独立 SMS 的交付报告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个服务中,当一个广播接收器在循环发送短信时,监听发送报告,如何区分每条发送的短信的发送报告?这类似于:如何获取每条短信的发送报告循环发送android?

In a service, when sending smses in a loop with a broadcast receiver listening for delivery reports, how to differentiate the delivery report of each sms sent? This is similar to : How to get Delivery Report of each SMS sent in loop android?

除了我认为他在活动中使用它而我在 getIntent() 无效的服务中使用它.

except that I think he's using it in an activity and I'm using it in a service where getIntent() is of no avail.

编辑 2:发布我的代码

Edit 2: posting my code

public class CheckServer extends Service
{
  public String snumber[] = new String[10];

  public JSONArray array;

  public int onStartCommand(Intent intent,int flags, int startid)
  {
    // Do useful things.

  ServiceAction SA = new ServiceAction();
  SA.execute();

  try 
  {
      SA.get();
  } 
  catch (InterruptedException e) 
  {
      e.printStackTrace();
  } 
  catch (ExecutionException e) 
  {
      e.printStackTrace();
  }


  new startSending().execute();

  scheduleNextUpdate();

   return START_STICKY;
}

public class startSending extends AsyncTask<Void,Void,Void>
{
    @Override
    protected Void doInBackground(Void... params) 
    {
        String no,message;

          try 
            {
                for (int i = 0; i < array.length(); i++) 
                {
                      JSONObject row;
                      row = array.getJSONObject(i);
                      snumber[i] = row.getString("sno");
                      no = row.getString("no");
                      message = row.getString("message");
                      sendSMS(no,message,snumber[i]);
                }
            } 
            catch (IllegalStateException e) 
            {
                e.printStackTrace();
            } 
            catch (JSONException e) 
            {
                e.printStackTrace();
            }

        return null;
    }
}

private void scheduleNextUpdate()
{
  Intent intent = new Intent(this, this.getClass());
  PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

// The update frequency should often be user configurable.  This is not.

long currentTimeMillis = System.currentTimeMillis();
long nextUpdateTimeMillis = currentTimeMillis + 1 * DateUtils.MINUTE_IN_MILLIS;
Time nextUpdateTime = new Time();
nextUpdateTime.set(nextUpdateTimeMillis);

  AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
  alarmManager.set(AlarmManager.RTC, nextUpdateTimeMillis, pendingIntent);
}

public class ServiceAction extends AsyncTask<Void,Void,Void>
{
@Override
protected Void doInBackground(Void... arg0) 
{
      HttpResponse response = null;
      HttpClient client = new DefaultHttpClient();
      HttpGet request = new HttpGet();
      try 
      {
        request.setURI(new URI("http://www.somesite.com/sms/getsms"));
        response = client.execute(request);
        String result = convertStreamToString(response.getEntity().getContent());
        array = new JSONArray(result);
      } 
      catch (URISyntaxException e) 
      {
        e.printStackTrace();
      } 
      catch (ClientProtocolException e) 
      {
        e.printStackTrace();
      } 
      catch (IOException e) 
      {
        e.printStackTrace();
      } 
      catch (JSONException e) 
      {
        e.printStackTrace();
      }


    return null;
}
}

public static String convertStreamToString(InputStream inputStream) throws IOException
{
  if (inputStream != null)
  {
      Writer writer = new StringWriter();

      char[] buffer = new char[1024];
      try
      {
          Reader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"),1024);
          int n;
          while ((n = reader.read(buffer)) != -1)
          {
              writer.write(buffer, 0, n);
          }
      }
      finally
      {
          inputStream.close();
      }
      return writer.toString();
  }
  else
  {
      return "";
  }
}


public void sendSMS(String number,String message,String serialnum) 
{   
  String SENT = "SMS_SENT";

  PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
      new Intent(SENT), 0);

  //---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;
          }

          unregisterReceiver(this);
      }
  }, new IntentFilter(SENT));

  String DELIVERED = "SMS_DELIVERED";

  Intent delivered = new Intent(DELIVERED);
  delivered.putExtra("MsgNum", serialnum);
  PendingIntent deliveredPI = PendingIntent.getBroadcast(this, Integer.parseInt(serialnum), delivered, 0);

    //---when the SMS has been delivered---
    registerReceiver(new BroadcastReceiver()
        {   
            @Override
           public void onReceive(Context context, Intent intent)
           {
                       switch (getResultCode())
                       {
                           case Activity.RESULT_OK:

                               Toast.makeText(getBaseContext(), "SMS delivered",Toast.LENGTH_SHORT).show();

                               updateSMSStatus USS =  new updateSMSStatus();

                               USS.execute(intent.getStringExtra("Msgnum"));

                               break;

                           case Activity.RESULT_CANCELED:

                               Toast.makeText(getBaseContext(), "SMS not delivered",Toast.LENGTH_SHORT).show();

                               break;                        
                       }

                     unregisterReceiver(this);
            }

        },
        new IntentFilter(DELIVERED)); 

    ContentValues values = new ContentValues();
    values.put("address", number);
    values.put("body", message);
    getContentResolver().insert(Uri.parse("content://sms/sent"), values);

    SmsManager smsMngr = SmsManager.getDefault();
    smsMngr.sendTextMessage(number, null, message, sentPI, deliveredPI);

}

public class updateSMSStatus extends AsyncTask<String,Void,Void>
{
    @Override
    protected Void doInBackground(String... params) {

          HttpResponse response = null;
          HttpClient client = new DefaultHttpClient();
          HttpGet request = new HttpGet();
          try 
          {
            Log.i("SMS APP", "MyClass.getView() — Serial Number = " + params[0]);
            request.setURI(new URI("http://www.somesite.com/sms/updatesmsstatus?uname=someone&sno="+params[0]));
            response = client.execute(request);
            String result = convertStreamToString(response.getEntity().getContent());
            Log.i("SMS APP","Update SMS Status is :"+result);
          } 
          catch (URISyntaxException e)
          {
            e.printStackTrace();
          } 
          catch (ClientProtocolException e) 
          {
            e.printStackTrace();
          } 
          catch (IOException e) 
          {
            e.printStackTrace();
          }

        return null;
    }
}

  @Override
  public IBinder onBind(Intent intent) 
  {
     return null;
  }

}

推荐答案

您将消息编号额外添加到 Intent 中,如下所示:

You add the message number extra to the Intent like this:

delivered.putExtra("MsgNum", serialnum);

然后您尝试像这样提取它:

and you try to extract it like this:

USS.execute(intent.getStringExtra("Msgnum"));

putExtra()中有大写N",在getStringExtra()中有小写n"强>.

这就是为什么你应该总是使用常量来处理这样的事情.它可以防止您花费数小时来尝试查找由印刷错误引起的错误.

This is why you should always use constants for stuff like this. It prevents you spending hours trying to find errors caused by typographical errors.

试试这个:

public static final String EXTRA_MSGNUM = "MsgNum";

然后使用:

delivered.putExtra(EXTRA_MSGNUM, serialnum);

和:

USS.execute(intent.getStringExtra(EXTRA_MSGNUM));

添加一些关于根据 OP 的评论生成不同 PendingIntent 的内容

Add something about generating different PendingIntents based on OP's comment

OP 在评论中写道:

我的错字不好,因此感觉像一只羊,我测试了它,它现在不给空值,而是给我序列号循环中为所有消息发送的第一条消息,如果我发送 1720 24 21 25 27,对于所有交付报告,它只给了我 17 分

My bad for the typo, felt like a sheep due to that, i tested it, it does not give a null value now, instead, it gives me the serial number of the first message sent in the loop for all messages, if i send 17 20 24 21 25 27, it gives me only 17 for all the delivery reports

您的问题是 PendingIntent 的工作方式.系统管理一个 PendingIntent 池.当您的代码执行此操作时:

Your problem is the way PendingIntent works. The system manages a pool of PendingIntents. When your code does:

String DELIVERED = "SMS_DELIVERED";
Intent delivered = new Intent(DELIVERED);
delivered.putExtra("MsgNum", serialnum);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this,
                    Integer.parseInt(serialnum), delivered, 0);

这会导致系统搜索与您传入的参数(在本例中为您的 Intent)匹配的 PendingIntent.但是,PendingIntent 使用的匹配算法仅比较 Intent 的某些字段,以确定它是否是您要查找的字段.特别是,它不会比较额外内容.所以这意味着在您创建第一个 PendingIntent 之后,对 PendingIntent.getBroadcast() 的调用将始终从池中返回相同的 PendingIntent(而不是创建一个新的,这正是您想要的).

This causes the system to search for a PendingIntent that matches the parameters you've passed in (in this case, your Intent). However, the matching algorithm that PendingIntent uses only compares certain fields of the Intent to determine if it is the one that you are looking for. In particular, it does not compare extras. So this means after you've created the first PendingIntent, the call to PendingIntent.getBroadcast() will always return the same PendingIntent from the pool (and not create a new one, which is what you want).

为了使 PendingIntent.getBroadcast() 的调用在每次调用时创建一个新的 PendingIntent,请尝试使传递给调用的参数唯一(例如:通过使 Intent 中的 ACTION 唯一).此外,由于这些 PendingIntent 中的每一个都只会使用一次,因此您应该在获取 PendingIntent 时设置 FLAG_ONE_SHOT,如下所示:

In order to make the call to PendingIntent.getBroadcast() create a new PendingIntent every time you call it, try making the parameters you pass to the call unique (for example: by making the ACTION in the Intent unique). Also, since each of these PendingIntents will only be used once you should set the FLAG_ONE_SHOT when obtaining the PendingIntent like this:

String DELIVERED = "SMS_DELIVERED" + serialnum; // Unique ACTION every time
Intent delivered = new Intent(DELIVERED);
delivered.putExtra("MsgNum", serialnum);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this,
                    Integer.parseInt(serialnum), delivered,
                    PendingIntent.FLAG_ONE_SHOT);

由于每次调用 PendingIntent.getBroadcast() 的 ACTION 都会不同,所以这应该可以解决您的问题.

Since the ACTION will be different for each call to PendingIntent.getBroadcast(), this should solve your problem.

根据评论中的讨论添加注册广播接收器的替代方法

如果您创建一个扩展 BroadcastReceiver 的类,您可以将其添加到清单中,然后您根本不需要显式注册广播接收器.像这样:

If you create a class that extends BroadcastReceiver, you can add that to the manifest and then you don't need to explicitly register the broadcast receiver at all. Something like this:

public class MessageStatusReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // This is called when the status of your SMS changes (delivery or send status)
        // .. put your code here ..
   }
}

在清单中声明接收者:

<receiver android:name=".MessageStatusReceiver" />

在发送短信的代码中,执行以下操作:

In your code that sends the SMS, do this:

String DELIVERED = "SMS_DELIVERED" + serialnum; // Unique ACTION every time
Intent delivered = new Intent(context, MessageStatusReceiver.class);
delivered.setAction(DELIVERED ); // Set action to ensure unique PendingIntent
delivered.putExtra("MsgNum", serialnum);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this,
                    Integer.parseInt(serialnum), delivered,
                    PendingIntent.FLAG_ONE_SHOT);

这篇关于区分两个独立 SMS 的交付报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆