2

我有以下代码,它给出了一个没有通过非法参数异常注册的接收器

public class CheckServer extends IntentService
{

public String snumber;

public CheckServer()
{
  super("CheckServer");
}


@Override
public void onCreate()
{
  super.onCreate();

  String DELIVERED = "SMS_DELIVERED";

  DeliveryReceiver dReceiver = new DeliveryReceiver();

  registerReceiver(dReceiver,new IntentFilter(DELIVERED));

}

@Override
public void onDestroy()
{
  super.onDestroy();

  DeliveryReceiver dReceiver = new DeliveryReceiver();

  unregisterReceiver(dReceiver);
  dReceiver = null;
}


@Override
protected void onHandleIntent(Intent intent)
{
  try
  {
    serviceAction();
  }
  catch (ClientProtocolException e)
  {
    e.printStackTrace();
  }
  catch (IOException e)
  {
    e.printStackTrace();
  }
  catch (URISyntaxException e)
  {
    e.printStackTrace();
  }
  catch (JSONException e)
  {
    e.printStackTrace();
  }

  scheduleNextUpdate();
}

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

  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 void serviceAction() throws ClientProtocolException, IOException, URISyntaxException, JSONException
{
  HttpResponse response = null;
  HttpClient client = new DefaultHttpClient();
      HttpGet request = new HttpGet();
      request.setURI(new URI("http://www.examplecom/sms/getsms"));
      response = client.execute(request);

      String result = convertStreamToString(response.getEntity().getContent());

      String no,message;

      JSONArray array = new JSONArray(result);

      for (int i = 0; i < array.length(); i++)
      {
        JSONObject row = array.getJSONObject(i);
        snumber = row.getString("sno");
        no = row.getString("no");
        message = row.getString("message");
        sendSMS(no , message);
      }
}

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 DELIVERED = "SMS_DELIVERED";

  PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
            new Intent(DELIVERED), 0);

  SmsManager smsM = SmsManager.getDefault();
  smsM.sendTextMessage(number, null, message, null, deliveredPI);
}

public class DeliveryReceiver extends BroadcastReceiver
{     
  @Override
   public void onReceive(Context context, Intent arg1)
  {
               switch (getResultCode())
               {
                   case Activity.RESULT_OK:

                       Toast.makeText(getBaseContext(), "SMS delivered",Toast.LENGTH_SHORT).show();
                        try
                        {
                                updateSMSstatus(snumber);
                        }
                        catch (ClientProtocolException e)
                        {
                                e.printStackTrace();
                        }
                        catch (URISyntaxException e)
                        {
                                    e.printStackTrace();
                        }
                        catch (IOException e)
                        {
                                            e.printStackTrace();
                        }

                       break;

                   case Activity.RESULT_CANCELED:

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

                       break;                        
               }

  }

  public void updateSMSstatus(String sno) throws URISyntaxException, ClientProtocolException, IOException
  {
      HttpResponse response = null;
      HttpClient client = new DefaultHttpClient();
      HttpGet request = new HttpGet();
      request.setURI(new URI("http://www.example.com/sms/updatesmsstatus?uname=someone&sno="+sno));
      response = client.execute(request);


  }
}

问题是即使我在 onCreate() 方法中注册它,我也会收到一个未收到的寄存器错误,可能是什么问题?

4

2 回答 2

3

您必须传递与一起使用的相同BroadcastReceiver对象。你不是。请保留您在 的数据成员中使用的,然后将相同的对象用于。unregisterReceiver()registerReceiver()BroadcastReceiverregisterReceiver()ServiceunregisterReceiver()

于 2012-10-07T11:48:59.687 回答
1

我没有在方法中注册onCreate()和取消注册,而是将方法更改为:onDestroy()sendSMS

public void sendSMS(String number,String message,String serialnum) 
{  
  String DELIVERED = "SMS_DELIVERED";

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

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

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

}

现在触发了发送短信的操作,谢谢大家。

关于如何区分每个短信的交付报告的任何想法?

我不太确定如何intent.putExtra()在我service的交付中使用该方法BroadcastReceiver

于 2012-10-08T11:34:46.700 回答