我正在使用本地广播让我的服务知道 AsyncTask 已完成其工作,但我有一个小问题:广播仅发送一次(它由仅在应用程序启动时调用的函数创建)但我收到两次。
简化代码:
@Override
protected void onPostExecute(HttpResponse result) {
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(getBaseContext());
localBroadcastManager.sendBroadcast(new Intent(getString(R.string.bc_CONNECTED)));
}
在服务中:
private BroadcastReceiver connectedBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(getString(R.string.app_tag), "broadcast received !!");
}
};
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
LocalBroadcastManager.getInstance(this).registerReceiver(connectedBroadcastReceiver, new IntentFilter(getString(R.string.bc_CONNECTED)));
return START_STICKY;
}
有没有人遇到过这种奇怪的行为?