4

i'd like to show a dialog, like when i use showDialog(...) on the Activity, but i need to do that on a widget, so i can't use showDialog(...). How can i do that?

I tried this on my onReceive method but it crash:

AlertDialog.Builder builder;
            builder = new AlertDialog.Builder(context);
            builder.setMessage("Are you sure?")
            .setCancelable(false)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    db.open();
                    db.remove_last();
                    db.close();
                    dialog.dismiss();
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
            builder.show();

Thanks, Valerio

Skatephone
  • 1,165
  • 2
  • 12
  • 26

2 Answers2

4

Widgets aren't intended to support that kind of interaction, and since they run under the UID of your launcher and not that of your application, they're not going to have access to the database. (That's probably the cause of your crash; look in the log to find out for sure.)

Launch an activity in your application that does what you want. If you want it to look like a dialog box, apply the Theme.Dialog style:

<activity
  android:name=".MyThing"
  android:label="@string/mythinglabel"
  android:theme="@android:style/Theme.Dialog"
/>
Blrfl
  • 6,817
  • 1
  • 25
  • 25
0

The dialog must come from an Activity and not from Widget.