IntentService会广播,但onReceive不会接收广播 [英] IntentService does broadcast but onReceive doesn't receive broadcast

查看:206
本文介绍了IntentService会广播,但onReceive不会接收广播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(注意),在本问题的末尾,我进行了编辑,其中我用一种答案替代了一种方法,以解决该问题.的onReceive从未被称为,并且添加了onDestroy来解决在解决第一个问题后出现的新问题.)

(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.)

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

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

数据库连接器:

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方法:

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

这里是MatchesActivity中的onDestroy(用于启动服务),在服务完成工作时需要调用:

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

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

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

MatchesActivity中的onCreate变得更简单,因为定义了mMessageReceiver:

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

推荐答案

我没有建立什么连接?

What connection(s) have I failed to make?

在您的第一段代码中,您正在使用LocalBroadcastManager.在第二段代码中,您不是.

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

替换:

this.sendBroadcast(intent);

具有:

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

这篇关于IntentService会广播,但onReceive不会接收广播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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