Skip to content Skip to sidebar Skip to footer

Showing An Ok Dialog Box From A Service/receiver In Android

My application has a receiver which is invoked when an SMS is received. I want to notify the user with a simple 1 button dialog box.. This is my code : AlertDialog.Builder builder

Solution 1:

First, you cannot display a dialog from a Service or BroadcastReceiver.

Second, please DO NOT INTERRUPT THE USER. The proper way to let the user know about something like this that occurred in the background is to display a Notification.

Solution 2:

Here is what I have done: from my service instance I start the activity like this:

final Intent dialog = newIntent(this, BackGroundDialogs.class);
dialog.setType("text/plain");
dialog.putExtra(android.content.Intent.EXTRA_TEXT, reason);
dialog.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity(dialog);

as for the BackGround Dialogs class, it looks like this:

/**
 * @ To create the illusion of a alert window displayed on its own when app is
 * in the background. Really, this is a Activity but its only displaying an
 * alert window and the Activity borders have been removed.
 */publicclassBackGroundDialogsextendsActivity {

    publicBroadcastReceiver receiver;
    publicAlertDialog mAlert;

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE); // custom theme with// no bordersIntentFilter filter = newIntentFilter();
        filter.addAction(Consts.DISMISS_DIALOG);// we can dismiss it via an// intent if we choose
        receiver = newBroadcastReceiver() {

            @OverridepublicvoidonReceive(Context context, final Intent intent) {
                // do something based on the intent's actionif (context == null) return;
                if (intent.getAction().equals(Consts.DISMISS_DIALOG)) {
                    finish();
                }
            }
        };
        registerReceiver(receiver, filter);
    }

    /**
     * @brief Shows an alert message using a Dialog window.
     * @paramreason
     *            :the message you wish to display in the alert
     */publicvoidshowAlert(final String reason) {
        mAlert = newAlertDialog.Builder(this).create();
        mAlert.setCancelable(false);
        TextViewMsg_tv = newTextView(this);
        Msg_tv.setTypeface(null, Typeface.BOLD);
        Msg_tv.setTextSize(16.0f);
        Msg_tv.setText(reason);
        Msg_tv.setGravity(Gravity.CENTER_HORIZONTAL);
        mAlert.setView(Msg_tv);
        mAlert.setButton("OK", newDialogInterface.OnClickListener() {

            @OverridepublicvoidonClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                finish();
            }
        });
        mAlert.show();
    }

    @OverrideprotectedvoidonResume() {
        super.onResume();
        Bundle extras = getIntent().getExtras();
        String reason = extras.getString(Intent.EXTRA_TEXT);
        if (reason.equalsIgnoreCase("DISMISS")) finish();
        elseshowAlert(reason);// invoke the new dialog to show
    }

    @OverrideprotectedvoidonPause() {
        super.onPause();
        if (mAlert != null) if (mAlert.isShowing()) mAlert.dismiss();
        finish();
    }

    @OverrideprotectedvoidonDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);
    }
}

you don't have to use the BroadcastReceiver calls, I just thought it might be handy to control the alert from somewhere else if required.

Solution 3:

Rahim... you can start the activity from the service the way you are starting activity from activiy

Intent newActivity = newIntent(context,servicename.class);
context.startService(newActivity);

Context should be application context

Solution 4:

You can do this by using TYPE_SYSTEM_ALERT:

AlertDialogalertDialog=newAlertDialog.Builder(this)
                    .setTitle("Title")
                    .setMessage("Hello dude")
                    .setCancelable(false)
                    .setPositiveButton("Got you", newDialogInterface.OnClickListener() {
                        @OverridepublicvoidonClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub//mp.stop();
                        }
                    }).create();

alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alertDialog.show();

Please note that you have to use the following permission:

<uses-permissionandroid:name="android.permission.SYSTEM_ALERT_WINDOW" />

Post a Comment for "Showing An Ok Dialog Box From A Service/receiver In Android"