如何在小部件上显示对话框?

How to show a dialog on a widget?(如何在小部件上显示对话框?)

本文介绍了如何在小部件上显示对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示一个对话框,就像我在 Activity 上使用 showDialog(...) 时一样,但我需要在小部件上执行此操作,所以我不能使用 showDialog(...).我该怎么做?

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?

我在我的 onReceive 方法上试过这个,但它崩溃了:

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();

谢谢,瓦莱里奥

推荐答案

小部件并非旨在支持这种交互,因为它们在启动器的 UID 下运行,而不是在应用程序的 UID 下运行,所以它们不是将有权访问数据库.(这可能是您崩溃的原因;请查看日志以确定.)

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.)

在您的应用程序中启动一个执行您想要的操作的活动.如果您希望它看起来像一个对话框,请应用 Theme.Dialog 样式:

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"
/>

这篇关于如何在小部件上显示对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在小部件上显示对话框?

基础教程推荐