Wun
Wun

Reputation: 6381

How to show the dialog on the top when APP is operation in background?

I have use the AlertDialog , but it only show the dialog when APP is open.

If the APP is run in background , the AlertDialog will not show on the top.

For example, clock APP.

It will show the dialog or a tip window on the top when time's up.

How to show the dialog on the top when APP is operation in background ???

Upvotes: 2

Views: 2857

Answers (2)

Sukru A.
Sukru A.

Reputation: 483

I know this is a bit late for response but since there is no accepted answer, you may consider this.

You don't have to create and add a custom view to window. If you aim to show just a regular dialog you can do as follows.

Create a Service class and let application know about this service via writing it to AndroidManifest.xml. Something like;

<service android:name="your.package.name.PopupService"/>

Then implement the service;

public class PopupService extends Service {


private Intent myIntent;
MaterialDialog dialog;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    this.myIntent = intent;
    showDialog(myIntent.getStringExtra("msg")); // you can send extra information to service via intent
    return super.onStartCommand(intent, flags, startId);
}

@Override
public void onCreate() {
    super.onCreate();

}

private void showDialog(String message)
{

    if (dialog != null) {
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    dialog = new MaterialDialog.Builder(getApplicationContext())
            .content(message)
            .positiveText("OK")
            .negativeText("cancel")
            .positiveColor(getResources().getColor(R.color.colorAccent))
            .negativeColor(getResources().getColor(R.color.colorPrimary))
            .cancelable(true)
            .callback(new MaterialDialog.ButtonCallback() {
                @Override
                public void onPositive(MaterialDialog dialog) {
                    stopSelf();
                }

                @Override
                public void onNegative(MaterialDialog dialog) {
                    stopSelf();
                }
            })
            .dismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    stopSelf();
                }
            })
            .build();

    dialog.getWindow().setType(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
            | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
            | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    dialog.show();

}

@Override
public void onDestroy() {
    if (dialog != null) {
        dialog.dismiss();
    }
    super.onDestroy();
}

}

And when you want to show the dialog just call the service, like;

Intent intent = new Intent(MainActivity.this, PopupService.class);
intent.putExtra("msg","Test Message");
startService(intent);

One more thing is, just add this permission to AndroidManifest.xml

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

Hope this helps :)

Upvotes: 1

kapil thadani
kapil thadani

Reputation: 1425

You can display system level alerts when your app is in background. Consider following code to show such alerts for e.g.

WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
                        | WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                        | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSPARENT);

        params.height = WindowManager.LayoutParams.MATCH_PARENT;
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.format = PixelFormat.TRANSLUCENT;

        params.gravity = Gravity.TOP;

        LinearLayout ly = new LinearLayout(context);
        ly.setBackgroundColor(Color.BLACK);
        ly.setOrientation(LinearLayout.VERTICAL);

        ImageView iv = new ImageView(context);
        iv.setImageResource(R.drawable.ic_launcher);

        ly.addView(iv);

        TextView tv1 = new TextView(context);
        tv1.setWidth(params.width);
        tv1.setBackgroundColor(Color.BLUE);

        ly.addView(tv1);

        wm.addView(ly, params);
  • add permission in manifest

    uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"

Upvotes: 1

Related Questions