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

时间:2013-11-12 06:28:53

标签: android dialog alertdialog android-alertdialog android-4.3-jelly-bean

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

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

例如,时钟APP。

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

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

2 个答案:

答案 0 :(得分: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);
  • 在清单中添加权限

    uses-permission android:name =“android.permission.SYSTEM_ALERT_WINDOW”

答案 1 :(得分: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"/>

希望这会有所帮助:)