触摸 Android 动态壁纸时打开对话框

发布于 2024-10-05 21:32:35 字数 1367 浏览 0 评论 0原文

我想在点击动态壁纸时打开一个对话框,其中包含一些简单的信息。在 Android 动态壁纸中覆盖 onCommand,并添加几乎直接从 Android 文档中直接取出的自定义对话框(未显示布局 info_dialog.xml):

@Override
public Bundle onCommand (String action, int x, int y, int z, Bundle extras, boolean resultRequested)
{
 System.out.println(action);  
 Context mContext = getApplicationContext();
 Dialog dialog = new Dialog(mContext);

 dialog.setContentView(R.layout.info_dialog);
 dialog.setTitle("Custom Dialog");

 TextView text = (TextView) dialog.findViewById(R.id.text);
 text.setText("Hello, this is a custom dialog!");

 dialog.show();

 return null
}    

只会生成一个异常:

12-02 07:14:40.880: ERROR/AndroidRuntime(295): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
12-02 07:14:40.880: ERROR/AndroidRuntime(295):     at android.view.ViewRoot.setView(ViewRoot.java:509)

12-02 07:14:40.880: ERROR/AndroidRuntime(295):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)

12-02 07:14:40.880: ERROR/AndroidRuntime(295):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)

12-02 07:14:40.880: ERROR/AndroidRuntime(295):     at android.app.Dialog.show(Dialog.java:241)

我假设这是因为我试图提高来自壁纸服务而不是活动的对话框。重写WallpaperService.Engine的onTouchEvent方法只会得到相同的结果。

这是否意味着我需要启动一个单独的活动来托管对话框?或者无法从动态壁纸触发对话框?

I’d like to open a dialog box with some simple information on it when a live wallpaper is tapped. Overriding onCommand in an Android live wallpaper, and adding a custom dialog almost straight out of the Android docs (with a layout info_dialog.xml not shown):

@Override
public Bundle onCommand (String action, int x, int y, int z, Bundle extras, boolean resultRequested)
{
 System.out.println(action);  
 Context mContext = getApplicationContext();
 Dialog dialog = new Dialog(mContext);

 dialog.setContentView(R.layout.info_dialog);
 dialog.setTitle("Custom Dialog");

 TextView text = (TextView) dialog.findViewById(R.id.text);
 text.setText("Hello, this is a custom dialog!");

 dialog.show();

 return null
}    

just generates an exception:

12-02 07:14:40.880: ERROR/AndroidRuntime(295): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
12-02 07:14:40.880: ERROR/AndroidRuntime(295):     at android.view.ViewRoot.setView(ViewRoot.java:509)

12-02 07:14:40.880: ERROR/AndroidRuntime(295):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)

12-02 07:14:40.880: ERROR/AndroidRuntime(295):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)

12-02 07:14:40.880: ERROR/AndroidRuntime(295):     at android.app.Dialog.show(Dialog.java:241)

I’m assuming this is because I’m trying to raise the dialog from a WallpaperService rather than from an Activity. Overriding the WallpaperService.Engine’s onTouchEvent method just gets the same result.

Does this mean that I need to spin up a separate Activity to host the dialog? Or is triggering a dialog from a live wallpaper not possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十级心震 2024-10-12 21:32:35

这是否意味着我需要启动一个单独的活动来托管对话框?

是的。或者,更好的是,使用对话主题的活动。

就我个人而言,如果您希望对整个动态壁纸执行此操作,我希望您会在市场上获得一大堆一星评级,因为我怀疑当您的活动/对话框不断弹出时,用户会感到恼火,因为他们错误地点击了主屏幕。

Does this mean that I need to spin up a separate Activity to host the dialog?

Yes. Or, better yet, use a dialog-themed activity.

Personally, if you are expecting to do this for your whole live wallpaper, I expect you will get a whole bunch of one-star ratings on the Market, as I suspect that users will get irritated when your activity/dialog keeps popping up just because they mis-tap on their home screen.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文