如何在service里面弹出对话框
先给一个需求:需要在service里面监听短信的接收,如果接收到短信了,弹出一个dialog来提示用户打开。

看看效果图:(直接在主桌面上弹出)
service里面弹出对话框


service中弹出提示框:
			AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
			alertDialog.setMessage("有新消息,是否查看?");
			alertDialog.setPositiveButton("否",
					new DialogInterface.OnClickListener()
					{
						public void onClick(DialogInterface dialog, int which)
						{
						}
					});
	
			alertDialog.setNegativeButton("是",
					new DialogInterface.OnClickListener()
					{
						public void onClick(DialogInterface dialog, int which)
						{
						}
					});
	
			ad = alertDialog.create();
			
	        ad.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);  
	        ad.setCanceledOnTouchOutside(false);//点击外面区域不会让dialog消失                               
	        ad.show();


可别忘了在manifest上加上权限:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

相关文章:

  • 2021-09-09
  • 2022-12-23
  • 2022-12-23
  • 2021-08-02
  • 2021-04-02
  • 2022-02-06
  • 2021-07-03
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-30
  • 2022-12-23
  • 2021-10-29
相关资源
相似解决方案