今天做项目有个需求就是有一个页面需要弹出一个dialog,但是这个dialog不可以影响,这个页面的跳转.这个页面可能跳转也可能不跳转,跳转后,这个dialog,还是显示的,
然而我们平时写的dialog是基于activity的,那么在这种情况下是不可能的,网上搜索了下,提出以下解决办法
在service中弹出dialog
public class ShowDialogService extends Service { @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); } @Override @Deprecated public void onStart(Intent intent, int startId) { // TODO Auto-generated method stub super.onStart(intent, startId); String str_username=intent.getExtras().getString("username"); String str_password=intent.getExtras().getString("password"); AlertViewDialog dialog=new AlertViewDialog(ShowDialogService.this); dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); dialog.show(); dialog.setTitle("提示"); String html="<p>已将您注册为会员</p><p>用户名:"+str_username+"</p><p>密 码:"+str_password+"</p>"; dialog.setMessage(Html.fromHtml(html).toString()); /* AlertDialog.Builder dialog=new AlertDialog.Builder(ShowDialogService.this); // TextView view=new TextView(ShowDialogService.this); View view=LayoutInflater.from(ShowDialogService.this).inflate(R.layout.slt_cnt_type, null); LinearLayout linear=(LinearLayout) view.findViewById(R.id.dialog_conent); LinearLayout.LayoutParams linearParams =(LinearLayout.LayoutParams) linear.getLayoutParams(); //取控件textView当前的布局参数 linearParams.height = 100;// 控件的高强制设成20 linearParams.width = 300; linear.setOrientation(LinearLayout.VERTICAL); linear.setLayoutParams(linearParams); TextView username=new TextView(ShowDialogService.this); TextView password=new TextView(ShowDialogService.this); username.setText("用户名:"); password.setText("密 码:"); linear.addView(username); linear.addView(password); dialog.setView(view); final AlertDialog d = dialog.create(); d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); d.show();*/ /* Window window=d.getWindow(); WindowManager.LayoutParams params = window.getAttributes(); params.dimAmount = 0f; window.setAttributes(params); */ }
这里的alertdialog 是自己写的一个继承的dialog。
这里弹出dialog 创建dialog的方式和以前写dialog的方式是一样的主要是加了一句话
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
dialog.show();
设置dialog为系统级别的,并且要在show之前