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

孤街醉人 提交于 2019-11-29 05:39:40

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:

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();
    }
}

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!