我使用过警报对话框,但它仅在APP打开时显示对话框。
如果APP在后台运行,警报对话框将不会显示在顶部。
例如,时钟应用程序。
当时间到了时,它将在顶部显示对话框或提示窗口。
当APP在后台运行时,如何在顶部显示对话框???
您可以在应用处于后台时显示系统级警报。考虑以下代码来显示此类警报,例如
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"
这有点晚了,但由于没有公认的答案,你可以考虑一下。
您不必创建自定义视图并将其添加到窗口。如果您只想显示常规对话框,则可以执行以下操作。
创建一个服务类,并通过将其写入 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"/>
希望这对:)有所帮助