IntentService does broadcast but onReceive doesn&#

2019-09-10 07:51发布

问题:

(NOTE that at the end of this Question I have an EDIT in which I have replaced one method with what the Answer said to do in order to fix the problem of onReceive never getting called and added onDestroy to fix a new problem that cropped up after fixing first problem.)

Here's how I attempted to capture the broadcast data, but onReceive never gets called since Log.w never displays anything:

public class MatchesActivity extends Activity implements DatabaseConnector.DatabaseProcessListener
{
  public static String SOME_ACTION = "com.dslomer64.servyhelperton.SOME_ACTION";
  public static String STRING_EXTRA_NAME = "match";

  @Override protected void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);

    LocalBroadcastManager.getInstance(this).registerReceiver
    (
        new BroadcastReceiver() 
        {
           @Override public void onReceive(Context context, Intent intent) 
           {
              String s = txaMatches.getText().toString() + intent.getStringExtra(STRING_EXTRA_NAME) ;
              txaMatches.setText(s);
              Log.w("MatchesActivity","`````onReceive <" + s + ">");
            }
        }, new IntentFilter(SOME_ACTION)
    );
    ...
    DatabaseConnector dbc = new DatabaseConnector(getApplicationContext(), assets);
    dbc.setDbProcesslistener(this); // set way to know matches has been defined
    dbc.findDBMatches();
  } // end onCreate
} // end MatchesActivity

Database connector:

public DatabaseConnector(Context _context, AssetManager _assets)
{
  mContext = _context;

//This method, called in `MatchesActivity` on button press, does start the service:

  public void findDBMatches()
  {
    Intent i= new Intent(mContext, QueryDB.class);
    mContext.startService(i);
  }

  // Here's the service:

  public static class QueryDB extends IntentService
  {
    public QueryDB()            { super(QueryDB.class.getSimpleName()); }
    public QueryDB(String name) { super(name); }

//Here's the procedure that does all the work (and it does execute):

    @Override protected void onHandleIntent(Intent intent) 
    { ...
      publishProgress(dicWord); // a String
    }

  //This does execute but it doesn't send `progress` back to `MatchesActivity`,
  //which initiated request for service (note: `publishProgress` is so named
  //because `QueryDB` used to be an `AsyncTask` and I just didn't change the name):

    protected void publishProgress(String progress) 
    {
      Intent intent = new Intent(MatchesActivity.SOME_ACTION);
      intent.putExtra(MatchesActivity.STRING_EXTRA_NAME, progress);

      this.sendBroadcast(intent); // THIS LINE IS THE PROBLEM, FIXED BELOW

      Log.w("DatabaseConnector", "`````publishProgress <" + progress + ">");
    }
}

What connection(s) have I failed to make?

EDIT

This is the CORRECTED method found just above:

    protected void publishProgress(String progress) 
    {
      Intent intent = new Intent(MatchesActivity.SOME_ACTION);
      intent.putExtra(MatchesActivity.STRING_EXTRA_NAME, progress);

      LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    }
}

Here is onDestroy in MatchesActivity (which starts the service), necessary to call when service has finished its work:

  @Override protected void onDestroy() 
  {
    super.onDestroy();
    LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
  }

Note that onDestroy refers to a new MatchesIntent variable, defined as:

  private BroadcastReceiver mMessageReceiver = new BroadcastReceiver()
  {
    @Override public void onReceive(Context context, Intent intent) 
    {
      String s = intent.getStringExtra(STRING_EXTRA_NAME) ;
      txaMatches.append(s + "\n");
    }
  };

And onCreate in MatchesActivity got simpler because of defining mMessageReceiver:

  @Override protected void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);

    LocalBroadcastManager.getInstance(this).registerReceiver
    (
       mMessageReceiver, new IntentFilter(SOME_ACTION)
    );
  }

回答1:

What connection(s) have I failed to make?

In your first block of code, you are using LocalBroadcastManager. In your second block of code, you are not.

Replace:

this.sendBroadcast(intent);

with:

LocalBroadcastManager.getInstance(this).sendBroadcast(intent);