How to trigger a Dialog from an Application instance?
i'm looking for a way to show a Dialog no matter what activity is shown.
I think what I'm trying to achieve is better explained using some code :
public class MyApp extends Application {
public 开发者_Python百科MyApplication() {
}
@Override
public void onCreate() {
super.onCreate();
boolean success = doSomeWebServiceCall();
if (!success)
showAlertDialog(); // fails with error
}
}
The error I am getting is :
ERROR/AndroidRuntime(375): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.view.ViewRoot.setView(ViewRoot.java:460)
Only an Activity
can display a Dialog
. Consider using a dialog-themed Activity
instead, which can be launched from your Application
object via startActivity()
.
I think a toast (could even throw in an image in this toast) is much more convenient if you don't need user interaction
Toast toast = Toast.makeText(getApplicationContext(), "YOUR TEXT HERE", Toast.LENGTH_LONG);
LinearLayout toastView = (LinearLayout) toast.getView();
ImageView infoImage = new ImageView(getApplicationContext());
infoImage.setImageResource(drawable.your_image);
toastView.addView(infoImage, 0);
toast.show();
精彩评论