android系统弹窗关闭
作者:admin发布时间:2021-07-27分类:收益展现浏览:评论:15
全局弹窗
终端开发总是会有这样的需求,无论在哪些场景下某种判断成立时候需要弹出通知比如网络断开等情况。类似这样的全局弹窗无非就是系统的或者悬浮窗,悬浮窗的话需要用户开启悬浮窗权限,这里我就只提一下的实现。这也是我在用的一种。
众所周知中依赖于,所以要显示全局弹窗,最终问题就是获取最上面的对象,这就涉及到的管理,一般会有在base中建立activity列表维护所有activity,我们这边使用在application生命周期监听函数中处理。
首先这里有一个顶部activity管理的单例类:
publicclassTopActivityManager{privatestaticfinalStringTAG="MyActivityManager";publicstaticTopActivityManagerinstance=newTopActivityManager();privateWeakReferenceActivitycurrentActivityWeakRef;publicTopActivityManager(){}publicstaticTopActivityManagergetInstance(){returninstance;}publicActivitygetCurrentActivity(){ActivitycurrentActivity=null;if(currentActivityWeakRef!=null){currentActivity=currentActivityWeakRef.get();}returncurrentActivity;}publicvoidsetCurrentActivity(Activityactivity){currentActivityWeakRef=newWeakReference(activity);LogMgr.d(TAG,currentActivityWeakRef.get().getClass().getSimpleName());}}
然后就是调用set更新该对象的时机:
privatevoidinitLifeCycle(){registerActivityLifecycleCallbacks(newActivityLifecycleCallbacks(){@OverridepublicvoidonActivityCreated(Activityactivity,BundlesavedInstanceState){LogMgr.d(TAG,"onActivityCreated");TopActivityManager.getInstance().setCurrentActivity(activity);}@OverridepublicvoidonActivityStarted(Activityactivity){LogMgr.d(TAG,"onActivityStarted");}@OverridepublicvoidonActivityResumed(Activityactivity){LogMgr.d(TAG,"onActivityResumed");TopActivityManager.getInstance().setCurrentActivity(activity);}@OverridepublicvoidonActivityPaused(Activityactivity){LogMgr.d(TAG,"onActivityPaused");}@OverridepublicvoidonActivityStopped(Activityactivity){LogMgr.d(TAG,"onActivityStopped");}@OverridepublicvoidonActivitySaveInstanceState(Activityactivity,BundleoutState){LogMgr.d(TAG,"onActivitySaveInstanceState");}@OverridepublicvoidonActivityDestroyed(Activityactivity){LogMgr.d(TAG,"onActivityDestroyed");}});}
这边可以看到在create和resume中都调用了set方法,resume调用是为了防止A启动B,B关闭后回到A的情况,在create时候设置是因为在有些情况进入页面oncreate里面就会做一些判断需要弹出全局弹窗,用到顶部activity,所以避免出错两个地方都进行设置。
这里再提供一份弹窗代码供测试
publicclassNetErrorDialog{privatestaticfinalStringTAG="NetErrorDialog";privatestaticNetErrorDialoginstance;@BindView(R.id.tvCancel)TextViewtvCancel;@BindView(R.id.tvNetSet)TextViewtvNetSet;privateMyDialognetErrorDialog;privateNetHandlernetHandler;publicstaticNetErrorDialoggetInstance(){if(instance==null){synchronized(NetErrorDialog.class){if(instance==null){instance=newNetErrorDialog();}}}returninstance;}privateNetErrorDialog(){netHandler=newNetHandler();}publicvoidshowDialog(){if(netErrorDialog!=nullnetErrorDialog.isShowing()){if(netErrorDialog.getContext()==TopActivityManager.getInstance().getCurrentActivity()){return;}else{dismiss();}}netErrorDialog=newMyDialog(TopActivityManager.getInstance().getCurrentActivity(),R.style.DialogTheme);ViewdlgView=LayoutInflater.from(TopActivityManager.getInstance().getCurrentActivity()).inflate(R.layout.dlg_net_error,null);ButterKnife.bind(this,dlgView);netErrorDialog.setContentView(dlgView);netErrorDialog.setCancelable(false);Windowwindow=netErrorDialog.getWindow();window.setGravity(Gravity.CENTER);window.setDimAmount(0f);window.setWindowAnimations(R.style.center_dialog_animStyle);window.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);WindowManager.LayoutParamsparams=window.getAttributes();params.width=DisplayUtils.getInstance().dpToPx(340);params.height=DisplayUtils.getInstance().dpToPx(140);window.setAttributes(params);tvCancel.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewv){netErrorDialog.dismiss();if(netHandler!=null){netHandler.sendMsg();}}});tvNetSet.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewv){IntentwifiSettingsIntent=newIntent("android.settings.WIFI_SETTINGS");wifiSettingsIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);TopActivityManager.getInstance().getCurrentActivity().startActivity(wifiSettingsIntent);}});netErrorDialog.show();}publicvoiddismiss(){if(netErrorDialog!=nullnetErrorDialog.isShowing()){netErrorDialog.dismiss();}}publicclassNetHandlerextendsHandler{privatestaticfinalintWHAT_NET_TIP=0x222;@OverridepublicvoidhandleMessage(Messagemsg){super.handleMessage(msg);if(msg.what==WHAT_NET_TIP){if(!NetUtils.isNetworkAvailable()){showDialog();}else{dismiss();}}}publicvoidsendMsg(){LogMgr.d(TAG,"startsenddelay");sendEmptyMessageDelayed(WHAT_NET_TIP,3*60*1000);}}publicvoidrelease(){if(netErrorDialog!=null){if(netErrorDialog.isShowing()){netErrorDialog.dismiss();}netErrorDialog=null;}instance=null;}}
相关阅读
标签:红米手机广告应用弹窗关闭
- 上一篇:华为手机怎样关闭广告弹窗
- 下一篇:完全屏蔽手机网页广告
你 发表评论:
欢迎- 收益展现排行
- 最近发表
已有15位网友发表了看法: