Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save codenameone/ecccc5c6452d055359f12374b2db2644 to your computer and use it in GitHub Desktop.
Save codenameone/ecccc5c6452d055359f12374b2db2644 to your computer and use it in GitHub Desktop.
package com.codename1.test.bgfetch;
import com.codename1.background.BackgroundFetch;
import com.codename1.components.SpanLabel;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Toolbar;
import com.codename1.util.Callback;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import com.codename1.io.ConnectionRequest;
import com.codename1.io.NetworkManager;
import com.codename1.io.services.RSSService;
import java.util.List;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.io.services.RSSService;
import com.codename1.ui.Container;
/**
* A simple demo showing the use of the Background Fetch API. This demo will load
* data from the Slashdot RSS feed while it is in the background.
*
* To test it out, put the app into the background (or select Pause App in the simulator)
* and wait 10 seconds. Then open the app again. You should see that the data is loaded.
*/
public class BackgroundFetchTest implements BackgroundFetch {
private Form current;
private Resources theme;
List<Map> records;
// Container to hold the list of records.
Container recordsContainer;
public void init(Object context) {
theme = UIManager.initFirstTheme("/theme");
// Enable Toolbar on all Forms by default
Toolbar.setGlobalToolbar(true);
// Pro only feature, uncomment if you have a pro subscription
// Log.bindCrashProtection(true);
}
public void start() {
if(current != null){
// Make sure we update the records as we are coming in from the
// background.
updateRecords();
current.show();
return;
}
Display d = Display.getInstance();
// This call is necessary to initialize background fetch
d.setPreferredBackgroundFetchInterval(10);
Form hi = new Form("Background Fetch Demo");
hi.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
Label supported = new Label();
if (d.isBackgroundFetchSupported()){
supported.setText("Background Fetch IS Supported");
} else {
supported.setText("Background Fetch is NOT Supported");
}
hi.addComponent(new Label("Records:"));
recordsContainer = new Container(new BoxLayout(BoxLayout.Y_AXIS));
//recordsContainer.setScrollableY(true);
hi.addComponent(recordsContainer);
hi.addComponent(supported);
updateRecords();
hi.show();
}
/**
* Update the UI with the records that are currently loaded.
*/
private void updateRecords() {
recordsContainer.removeAll();
if (records != null) {
for (Map m : records) {
recordsContainer.addComponent(new SpanLabel((String)m.get("title")));
}
} else {
recordsContainer.addComponent(new SpanLabel("Put the app in the background, wait 10 seconds, then open it again. The app should background fetch some data from the Slashdot RSS feed and show it here."));
}
if (Display.getInstance().getCurrent() != null) {
Display.getInstance().getCurrent().revalidate();
}
}
public void stop() {
current = Display.getInstance().getCurrent();
if(current instanceof Dialog) {
((Dialog)current).dispose();
current = Display.getInstance().getCurrent();
}
}
public void destroy() {
}
/**
* This method will be called in the background by the platform. It will
* load the RSS feed. Note: This only runs when the app is in the background.
* @param deadline
* @param onComplete
*/
@Override
public void performBackgroundFetch(long deadline, Callback<Boolean> onComplete) {
RSSService rss = new RSSService("http://rss.slashdot.org/Slashdot/slashdotMain");
NetworkManager.getInstance().addToQueueAndWait(rss);
records = rss.getResults();
System.out.println(records);
onComplete.onSucess(Boolean.TRUE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment