IntentService进行广播,但onReceive没有收到广播

时间:2016-05-01 20:14:31

标签: java android android-broadcast android-intentservice android-broadcastreceiver

(注意,在本期课程的最后,我有一个编辑,其中我已经用答案所说的做法替换了一个方法来解决问题onReceive永远不会被称为添加onDestroy来解决修复第一个问题后出现的新问题。

以下是我尝试捕获广播数据的方式,但onReceive永远不会被调用,因为Log.w从不显示任何内容:

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

数据库连接器:

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 + ">");
    }
}

我没有做出什么联系?

修改

这是上面找到的CORRECTED方法:

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

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

以下是onDestroy中的MatchesActivity(启动服务),这是服务完成工作时调用所必需的:

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

请注意,onDestroy引用了一个新的MatchesIntent变量,定义为:

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

由于定义onCreateMatchesActivity中的mMessageReceiver变得更加简单:

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

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

1 个答案:

答案 0 :(得分:1)

  

我没有做出什么联系?

在您的第一个代码块中,您正在使用LocalBroadcastManager。在你的第二段代码中,你不是。

替换:

this.sendBroadcast(intent);

使用:

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