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"
/>
这篇关于如何在小部件上显示对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在小部件上显示对话框?


基础教程推荐
- 如何使 UINavigationBar 背景透明? 2022-01-01
- :hover 状态不会在 iOS 上结束 2022-01-01
- 使用 Ryzen 处理器同时运行 WSL2 和 Android Studio 2022-01-01
- 在 iOS 上默认是 char 签名还是 unsigned? 2022-01-01
- 如何使用 YouTube API V3? 2022-01-01
- LocationClient 与 LocationManager 2022-01-01
- 固定小数的Android Money Input 2022-01-01
- Android ViewPager:在 ViewPager 中更新屏幕外但缓存的片段 2022-01-01
- Android文本颜色不会改变颜色 2022-01-01
- “让"到底是怎么回事?关键字在 Swift 中的作用? 2022-01-01