0

I've created this code based on work I have done in activities and from other questions here on SO. I get no errors. Just a warning message:

04-30 19:19:26.063: W/BroadcastQueue(480): 
Unable to launch app com.test.application/10091
for broadcast Intent { act=android.appwidget.action.APPWIDGET_ENABLED flg=0x10 
cmp=com.test.application/.TestWidget }: process is bad

Followed by this to do with the update action:

04-30 19:19:26.063: W/BroadcastQueue(480): Unable to launch app
com.test.application/10091 for broadcast Intent {   
act=android.appwidget.action.APPWIDGET_UPDATE flg=0x10 
cmp=com.test.application/.TestWidget (has extras) }: process is bad

and then (I don't know if this is part of it, it shows up in blue):

04-30 19:19:34.073: D/Launcher.Model(752): DbDebug   
Add item (null) to db, id: 126 (-100, 1, 1, 3)

Here is my code from the class TestWidget:

package com.test.application;

public class TestWidget extends AppWidgetProvider {

static DBHelper dbhelper;

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
    // TODO Auto-generated method stub
    super.onUpdate(context, appWidgetManager, appWidgetIds);

    updateWidgetContent(context, appWidgetManager, appWidgetIds);
}

public static void updateWidgetContent(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

    dbhelper.openDatabase();

    String name = "basestring";
    Cursor c = getDatabaseHelper(context).getReadableDatabase().rawQuery("SELECT * FROM websites ORDER BY RANDOM()", null);
    name = c.getString(c.getColumnIndex("weburl"));

    RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.main);
    remoteView.setTextViewText(R.id.TextView1, name);
    dbhelper.close();

    appWidgetManager.updateAppWidget(appWidgetIds, remoteView);
}

private static DBHelper getDatabaseHelper(Context context) {
    DBHelper dbhelper = null;
    if (dbhelper == null) {
        dbhelper = new DBHelper(context);
        dbhelper.openDatabase();
    }
    return dbhelper;
}   
}

I took code from my experiance with database and Activities and some from here: Android: get Widget Data from database

I've never worked with pulling data from a database for widgets. Either way I'm not getting any direct errors, the widget can be placed, it's just empty where the textview should be filled.

The database I'm using has over 2000 items so it's definitely not null. I want it to just show one. I've done this perfectly in an Activity with a button. (Press the button and one result from the db shows up in a textview).

I would really love some help. I feel like I'm very close.

EDIT: I plan on making this open source when I get get it working, I can't see any other open source projects out there that help with this sort of stuff so if I get it working I can share it with everyone.

Community
  • 1
  • 1
RED_
  • 2,997
  • 4
  • 40
  • 59

2 Answers2

0

It looks like you are trying to fetch data from db in launcher process (widget works "under" main launcher process), hence the exception telling that "process is bad".

In order to fetch data from DB and show them in widgetm I would suggests you to use RemoteViewsService and RemoteViewsFactory classes.They allow you to create and update widget using views like ListView or StackView.

For more information please refere to this examples.

Filip Zymek
  • 2,626
  • 4
  • 22
  • 31
  • So is my code fine in that case? Just that it needs to be wrapped in a RemoteViewService? It will probably take me awhile to get my head around this but if it's the easiest option then I will see what I can do. I only want to show one line of the data at a time, I can add a button to get another line of data from the database. – RED_ May 01 '13 at 16:19
0

I fixed this myself in the end. I'll make my project open-source at some point because there is barely any details out there. I basically had to call the createDatabase() method on top of the open and close database. I guess it wasn't being called.

RED_
  • 2,997
  • 4
  • 40
  • 59