How to show Dialog Box from the class which extends Application in android?

前端 未结 3 1653
谎友^
谎友^ 2020-12-17 10:21

I want show a dialog box after specific condition , but for demo right now I want show a Dialog Box from the class which extends Application . here is my code



        
相关标签:
3条回答
  • 2020-12-17 10:45

    You can't use an application [or service] context. If you really want to show you diallog from an application, you will have to pass an Activity context to it. You could also store the Activity context, but I do not recommend that. The activity context is voided on finish, so you will break your program. As @LeoLink said, just call it directly from your Activity.

    EDIT For Example

    class MyDialog {
        public Dialog show(Context context) {
            Dialog d = new Dialog(context);
            d.setTitle("I'm a dialog");
            d.setMessage("I'm a message");
            return d.show();
        }
    }
    
    0 讨论(0)
  • 2020-12-17 10:48

    Your program may behave as you want!**

    ** Just remember that you need to think about the consequences of its actions.

    public class MyApplication extends Application {
            /** 
            * show example alertdialog on context -method could be moved to other class 
            * (eg. MyClass) or marked as static & used by MyClas.showAlertDialog(Context)
            * context is obtained via getApplicationContext() 
            */
            public void showAlertDialog(Context context) {
                /** define onClickListener for dialog */
                DialogInterface.OnClickListener listener 
                      = new   DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    // do some stuff eg: context.onCreate(super)
                    }
                };
    
                /** create builder for dialog */
                AlertDialog.Builder builder = new AlertDialog.Builder(context)
                    .setCancelable(false)
                    .setMessage("Messag...")
                    .setTitle("Title")
                    .setPositiveButton("OK", listener);
                /** create dialog & set builder on it */
                Dialog dialog = builder.create();
                /** this required special permission but u can use aplication context */ 
                dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
                /** show dialog */
                dialog.show();
            }
    
            @Override
            public void onCreate() {
                showAlertDialog(getApplicationContext());
            }
    }
    

    imports for abowe:

    import android.app.AlertDialog;
    import android.app.Application;
    import android.app.Dialog; 
    import android.content.Context;
    import android.content.DialogInterface; 
    import android.view.WindowManager;
    

    edity:

    You cannot **display an application window/dialog through a Context that is not an Activity or Service. Try passing a valid activity reference

    ** u can use application context to create dialog by adding before call to Dialog.show();

    Dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); 
    
    - but this requires permission:  
    
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    

    Ref:

    • Dialogs
    • AlertDialog
    • WindowType
    0 讨论(0)
  • 2020-12-17 10:57

    The Application class is there to hold data that can be used by your activities, not to interact with the UI. Display the dialog from the activity you want it displayed in.

    EDIT: If you want to call the code from multiple activities, you can have a superclass for these activities, that contain the code with the dialog. Then extend this superclass in all the activities you want to display the dialog, and call it from there.

    0 讨论(0)
提交回复
热议问题