Skip to content

Instantly share code, notes, and snippets.

@yanguango
Created June 18, 2011 08:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yanguango/1032934 to your computer and use it in GitHub Desktop.
Save yanguango/1032934 to your computer and use it in GitHub Desktop.
public class RefreshService extends Service {
public void refreshTimeline() {
//build the intent with new messages
this.sendOrderedBroadcast(intent, null);
}
}
public class TimelineActivity {
private RefreshTimelineReceiver refreshReceiver;
private RefreshTimelineAndNotifyReceiver refreshAndNotifyReceiver;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IntentFilter filter = new IntentFilter(RefreshService.REFRESH_TIMELINE);
filter.setPriority(FILTER_LOW_PRIORITY);
refreshAndNotifyReceiver = new RefreshTimelineAndNotifyReceiver();
registerReceiver(refreshAndNotifyReceiver, filter);
}
public void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter(RefreshService.REFRESH_TIMELINE);
filter.setPriority(FILTER_HIGH_PRIORITY);
refreshReceiver = new RefreshTimelineReceiver();
registerReceiver(refreshReceiver, filter);
}
public void onPause() {
super.onPause();
unregisterReceiver(refreshReceiver);
}
public void onDestroy() {
super.onDestroy();
unregisterReceiver(refreshAndNotifyReceiver);
}
public class RefreshTimelineReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
showNewMessages();
this.abortBroadcast();
}
}
public class RefreshTimelineAndNotifyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
showNewMessages();
showNotification();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment