我有一个在后台运行的更新程序服务,并在发现更新时保持广播意图。我有一个活动(时间轴活动),它根据Updater Service提供的更新显示一些数据。我有另一个与时间线活动相关联的广播接收器,它监听更新器服务广播的意图。现在,只要Updater Service获取新更新,它就会正确地广播意图,并且也会被与时间轴活动关联的Receiver接收。问题是在收到广播后,UI上的数据没有刷新。负责更新的SimpleCursorAdapter在接收方的notifyDataSetChanged()
中调用onReceive()
后没有做任何事情。
以下是TimelineActivity.java的代码
public class TimelineActivity extends BaseActivity {
public static final String TAG = "TimelineActivity";
private Cursor cursor;
private ListView listTimeline;
//Adapter for connecting ListView and the cursor containing info from database
private SimpleCursorAdapter timelineAdapter;
static final String[] FROM = { DBHelper.C_CREATED_AT, DBHelper.C_USER,
DBHelper.C_TEXT };
static final int[] TO = { R.id.textCreatedAt, R.id.textUser, R.id.textText };
YambaApplication yamba;
private TimelineReceiver timelineReceiver = null;
private IntentFilter intentFilter = new IntentFilter("com.pack.android.yamba.NEW_STATUS");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.timeline);
//Fetch the views
listTimeline = (ListView) findViewById(R.id.listTimeline);
yamba = (YambaApplication) getApplication();
if(yamba.getSharedPreferences().getString("username", null) == null)
{
startActivity(new Intent(this, PrefsActivity.class)); //
Toast.makeText(this, R.string.msgSetupPrefs, Toast.LENGTH_LONG).show();
}
// Check if preferences have been set
if (yamba.getSharedPreferences().getString("username", null) == null) { //
startActivity(new Intent(this, PrefsActivity.class));
Toast.makeText(this, R.string.msgSetupPrefs, Toast.LENGTH_LONG).show();
}
timelineReceiver = new TimelineReceiver();
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "On Destroy of Timeline Activity called !");
//close the database
yamba.getStatusData().close();
}
//TODO : check for onWindowFocusChanged(boolean hasFocus) method for detailed test
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "On Resume of Timeline Activity called !");
setUpList();
Log.d(TAG, "Setting up list completed !");
registerReceiver(timelineReceiver, intentFilter);
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "On Pause of Timeline Activity called !");
Log.d(TAG, "In onPause timelineAdapter.getCursor().close() "+timelineAdapter.getCursor());
timelineAdapter.getCursor().close();
Log.d(TAG, "Closed timelineAdapter.getCursor");
Log.d(TAG, "In onPause cursor = "+cursor);
cursor.close();
Log.d(TAG, "Closed cursor");
unregisterReceiver(timelineReceiver);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
Log.d(TAG, "Does Timeline Activity has focus ? "+ hasFocus);
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "OnStop of Timeline Activity called !");
Log.d(TAG, "In onStop timelineAdapter.getCursor().close() "+timelineAdapter.getCursor());
timelineAdapter.getCursor().close();
Log.d(TAG, "Closed timelineAdapter.getCursor");
Log.d(TAG, "In onStop cursor = "+cursor);
cursor.close();
Log.d(TAG, "Closed cursor");
}
private void setUpList() {
// Get the data from the database
// TODO : verify for honeycomb which will use loaderManagers for managing lifecycle of a cursor
Log.d(TAG, "Setting up the list");
cursor = yamba.getStatusData().getStatusUpdates();
Log.d(TAG, "cursor is "+ cursor);
startManagingCursor(cursor);
// Setup Adapter
timelineAdapter = new SimpleCursorAdapter(this, R.layout.row, cursor, FROM, TO);
Log.d(TAG, "Timeline adapter is "+ timelineAdapter);
Log.d(TAG, "Count of data = " + timelineAdapter.getCount());
Log.d(TAG, "Is cursor empty ? " + timelineAdapter.isEmpty());
timelineAdapter.setViewBinder(VIEW_BINDER); //
listTimeline.setAdapter(timelineAdapter);
}
static final ViewBinder VIEW_BINDER = new ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if(view.getId() != R.id.textCreatedAt)
return false;
// Update the created at text to relative time
long timeStamp = cursor.getLong(columnIndex);
CharSequence relativetime = DateUtils.getRelativeTimeSpanString(timeStamp);
((TextView)view).setText(relativetime);
return true;
}
};
class TimelineReceiver extends BroadcastReceiver
{
private static final String TAG = "TimelineReceiver";
@Override
public void onReceive(Context context, Intent intent)
{
Log.d(TAG, "Just before requery, cursor is " + cursor);
yamba.getStatusData().getCursor().requery(); //
Log.d(TAG, "Just after requery");
timelineAdapter.notifyDataSetChanged(); //
onResume();
Log.d(TAG, "on Receive of "+TAG);
}
}
}
UpdaterService中的以下代码是广播意图:
Intent intent = new Intent("com.pack.android.yamba.NEW_STATUS");
String NEW_STATUS_EXTRA_COUNT = "com.pack.android.yamba.EXTRA_COUNT";
intent.putExtra(NEW_STATUS_EXTRA_COUNT, newUpdates);
updaterService.sendBroadcast(intent);
有人可以提出建议吗?
谢谢, Adithya。
答案 0 :(得分:1)
timelineReceiver = new TimelineReceiver();
registerReciever(timelineReciever,new IntentFilter("EXTRA_COUNT"));
请注册接收者。