琢磨了老半天,试了一下网上说的一种方法,我发现不行,这种方法是这样的:
1.首先注册静态广播
2.在清单中添加权限
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
3.然后在show()之前设置Dialog类型为TYPE_SYSTEM_ALERT
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
然后发现还是不行。这就郁闷啦!网上有人说用动态注册方式可以实现在广播接收者中弹出对话框,于是便试了一下,果然可以
首先开始注册广播,并给广播消息
private BroadcastReceiver broadcastReceiver;
broadcastReceiver=new tipsBroadReciver(); IntentFilter intentFilter=new IntentFilter(); intentFilter.addAction(ACTION); context.registerReceiver(broadcastReceiver,intentFilter); Intent intent=new Intent(); intent.setAction(ACTION); intent.putExtra("state","1"); context.sendBroadcast(intent);
在广播接收者中打开对话框
String state=intent.getExtras().getString("state"); AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context); dialogBuilder.setTitle("提示"); if(state.equals("1")){ text="没有查找到该好友"; } dialogBuilder.setMessage(text); dialogBuilder.setCancelable(false); dialogBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { context.unregisterReceiver(tipsBroadReciver.this); } }); AlertDialog alertDialog = dialogBuilder.create(); alertDialog.show();