Opening a dialog when an Android live wallpaper is touched
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 wallpa开发者_运维技巧per, 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?
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.
精彩评论