Android Widget load Data from XML on Initial Setup
<p>I am getting confused a bit on widgets I think. My goal is to have the widget update initially onStart or when the user first puts it on the screen. I am getting my data from an XML file that I am parsing within the service. It's working, but not 100%.</p>

<p>Currently the file is parsed from the XML and the getRose() runs before my Async query for the rose is done: </p>

<pre><code>views.setImageViewBitmap(R.id.ivdangerrose, getRose());
</code></pre>

<p>...happens before geRose() can happen...</p>

<p>So, I have tried to add all the code to get the rose within the async callback, however, I am running in the context issue... Maybe I am going about this all wrong. Any advice or help would be appreciated!</p>

<p>Basically, I want to have the user put the widget on the screen, the widget runs the xml parse, grabs the file, posts it to the users sdcard, checks for a new image now and again (or at a certain time, that's another question...), shows the file in the ImageView. </p>

<p>Here is my Service:</p>

<pre><code>public class SacWidgetService extends Service {
    private static final String TAG = "WIDGET SERVICE";
    AQuery aq;
    private String roseXML = "http://www.domain.com/roseXML.xml";
    private String roseUrl;
    private AppWidgetManager appWidgetManager;
    private int[] allWidgetIds;
    @Override
    public void onStart(Intent intent, int startId) {

        aq = new AQuery(this);
        // Create some random data

        appWidgetManager = AppWidgetManager.getInstance(this
                .getApplicationContext());

        allWidgetIds = intent
                .getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);

        xml_ajax();

        super.onStart(intent, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    public void xml_ajax() {
        aq.ajax(roseXML, XmlDom.class, this, "sacCb");
    }

    public void sacCb(String url, XmlDom xml, AjaxStatus status) {

        XmlDom r = xml.tag("reportrose");
        roseUrl = r.tag("img").attr("src").trim();

        File ext = Environment.getExternalStorageDirectory();
        File target = new File(ext,
                "rose/sac/dangerrose.png");

        aq.download(roseUrl, target, new AjaxCallback&lt;File&gt;() {

            public void callback(String url, File file, AjaxStatus status) {
                String s = status.getMessage();
                int i = status.getCode();
                String e = s + " | Status Code: " + i;

                if (file != null) {
                    Log.i("Widget SAC ROSE DL", s);

                    File ext = Environment.getExternalStorageDirectory();
                    File f = new File(ext,
                            "rose/sac/dangerrose.png");
                    Date dlm = new Date(f.lastModified());
                    String lm = dlm.toLocaleString();

                    for (int appWidgetId : allWidgetIds) {
                        RemoteViews views = new RemoteViews(this
                                .getApplicationContext().getPackageName(),
                                R.layout.sac_widget_layout);

                        Intent clickIntent = new Intent(this
                                .getApplicationContext(), SACActivity.class);
                        PendingIntent clickapp = PendingIntent.getActivity(
                                this.getApplicationContext(), 0, clickIntent,
                                PendingIntent.FLAG_UPDATE_CURRENT);

                        views.setOnClickPendingIntent(R.id.ivdangerrose,
                                clickapp);
                        views.setTextViewText(R.id.tvupdated, "Updated: " + lm);
                        views.setImageViewBitmap(R.id.ivdangerrose, getRose());
                        appWidgetManager.updateAppWidget(appWidgetId, views);
                    }
                    stopSelf();

                } else {
                    Log.e("Widget SAC ROSE DL", e);
                }
            }

        });
    }

    private Bitmap getRose() {
        Bitmap bitmap = null;

        File ext = Environment.getExternalStorageDirectory();
        File file = new File(ext,
                "rose/sac/dangerrose.png");
        try {
            FileInputStream input = new FileInputStream(file);
            bitmap = BitmapFactory.decodeStream(input);
            try {
                input.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                Log.e(TAG, e.getMessage());
            }
        } catch (FileNotFoundException e) {
            Log.e(TAG, e.getMessage());
        }

        return bitmap;

    }

}
</code></pre>

<p>EDIT:::</p>

<p>Well, I might have figured it out a little. I added a method called getInfo() and call it in the Callback:</p>

<pre><code>private void getInfo() {
        // TODO Auto-generated method stub
        File ext = Environment.getExternalStorageDirectory();
        File file = new File(ext,
                "avalancheconditionsreport/sac/dangerrose.png");
        Date dlm = new Date(file.lastModified());
        String lm = dlm.toLocaleString();

        for (int appWidgetId : allWidgetIds) {
            RemoteViews views = new RemoteViews(this.getApplicationContext()
                    .getPackageName(), R.layout.sac_widget_layout);

            Intent clickIntent = new Intent(this.getApplicationContext(),
                    SACActivity.class);
            PendingIntent clickapp = PendingIntent.getActivity(
                    this.getApplicationContext(), 0, clickIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);

            views.setOnClickPendingIntent(R.id.ivdangerrose, clickapp);
            views.setTextViewText(R.id.tvupdated, "Updated: " + lm);
            views.setImageViewBitmap(R.id.ivdangerrose, getRose());
            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }
</code></pre>
