我正在尝试创建显示在Dialog
顶部的NotificationArea
,并且与Dialog
进行交互不会关闭NotificationArea。说明我想要的一个很好的例子是Play商店中提供的BubbleUPNP
应用程序。
因此,当我单击标记的按钮时,将显示对话框,并且可以同时交互对话框和通知。
所以,我试过了:
我创建了一个Notification
并在其中一个按钮上设置了PendingIntent
Intent volumeUpIntent = new Intent(context, NotificationButtonListener.class);
volumeUpIntent.setAction("setVolume");
PendingIntent pendingVolumeUpIntent = PendingIntent.getBroadcast(context, 0, volumeUpIntent, 0);
remoteView.setOnClickPendingIntent(R.id.volumeIBtn, pendingVolumeUpIntent);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setSmallIcon(android.R.drawable.ic_media_play);
builder.setAutoCancel(false);
builder.setOngoing(true);
builder.setContentInfo(getResources().getText(R.string.drag_to_expand));
notification = builder.build();
mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notification.bigContentView = remoteView;
notification.priority = Notification.PRIORITY_MAX;
mNM.notify(1, notification);
然后,我创建了一个
BroadcastListener
,它捕获了Notification
的动作:public static class NotificationButtonListener extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals("setVolume")) {
Intent volumeIntent = new Intent(context, VolumeActivity.class);
volumeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(volumeIntent);
}
当我单击
Notification
中的按钮时,出现以Activity
为主题的Dialog
,但是当我尝试与Dialog
交互时,NotificationArea
关闭。有什么建议,如何实现我的目标? 最佳答案
听起来您需要使用WindowManager添加控件视图。我认为您要查找的窗口类型是TYPE_SYSTEM_OVERLAY
。
请参见WindowManager,WindowManager.LayoutParams