0

我使用了 AlertDialog ,但它只在 APP 打开时显示对话框。

如果 APP 在后台运行,则 AlertDialog 不会显示在顶部。

比如时钟APP。

时间到了,它会在顶部显示对话框或提示窗口。

APP在后台运行时如何在顶部显示对话框???

4

2 回答 2

1

当您的应用程序处于后台时,您可以显示系统级别的警报。考虑以下代码以显示此类警报,例如

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);
  • 在清单中添加权限

    使用权限 android:name="android.permission.SYSTEM_ALERT_WINDOW"

于 2013-11-12T06:37:38.640 回答
0

我知道这有点晚了,但由于没有可接受的答案,您可以考虑这一点。

您不必创建自定义视图并将其添加到窗口。如果您打算只显示一个常规对话框,您可以执行以下操作。

创建一个 Service 类并通过将其写入 AndroidManifest.xml 让应用程序知道该服务。就像是;

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

然后实现服务;

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

}

当你想显示对话框时,只需调用服务,比如;

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

还有一件事是,只需将此权限添加到 AndroidManifest.xml

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

希望这可以帮助 :)

于 2015-07-09T08:32:00.077 回答