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

}

SMS配信アクションがトリガーされるようになりました。ありがとうございます。

各SMSの配信レポートを区別する方法についてのアイデアはありますか?

配信でこのintent.putExtra()メソッドを使用する方法がよくわかりません。serviceBroadcastReceiver

于 2012-10-08T11:34:46.700 に答える