gpt4 book ai didi

java - IntentService确实广播但onReceive没有收到广播

转载 作者:行者123 更新时间:2023-12-01 09:58:46 25 4
gpt4 key购买 nike

(注意,在这个问题的末尾,我有一个编辑,其中我用答案所说的方法替换了一种方法,以解决问题onReceive 从未被调用并且添加了 onDestroy 来修复修复第一个问题后出现的新问题。)

以下是我 try catch 广播数据的方法,但 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 + ">");
}
}

我未能建立哪些连接?

编辑

这是上面找到的更正方法:

    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(启动服务),当服务完成其工作时需要调用:

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

由于定义了 mMessageReceiverMatchesActivity 中的 onCreate 变得更简单:

  @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。在你的第二个代码块中,你不是。

替换:

this.sendBroadcast(intent);

与:

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

关于java - IntentService确实广播但onReceive没有收到广播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36971432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com