57、android 應用內全局通知的實現方法


1、后台運行一個服務 間隔5s從服務器獲取一次數據,根據業務需要,當需要提醒給用戶時,從右側自動划出

類似效果如下:在任何界面都會有通知彈窗

 

2、實現過程

 

 

 

①android的根布局叫dector(獲取方式為:window.getDecotrView()),

②在activity中setContView設置的activity布局,可以通過dector.getChildAt(0)

③自定義一個FramLayout,FramLayout首先添加above;自定義通知內容,添加到FramLayout;然后把我們自定義的FrameLayout添加到dector

for example:

 1         ViewGroup decor = (ViewGroup) mWindow.getDecorView();//獲取根布局
 2         ViewGroup above = (ViewGroup) decor.getChildAt(0);//獲取activity的布局
 3         decor.removeView(above);
 4         above.setBackgroundDrawable(decor.getBackground());
 5      /**自定義通知開始**/
 6         LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 7 
 8         RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
 9         rlNotifactions.setLayoutParams(lp);
10 
11         View grid = inflater.inflate(R.layout.listview_notification,null);
12         ListView listview = (ListView)grid.findViewById(R.id.listview_notification_list);
13         listview.setAdapter(adapter);
14         RelativeLayout.LayoutParams lpTv = new RelativeLayout.LayoutParams(790, ViewGroup.LayoutParams.MATCH_PARENT);
15         lpTv.addRule(RelativeLayout.ALIGN_PARENT_TOP|RelativeLayout.ALIGN_PARENT_RIGHT);
16         lpTv.setMargins(0, 100, 0, 0);
      /**自定義通知結束**/
17 rlNotifactions.addView(grid,lpTv); 18 19 mAboveView.addView(above);
22 addView(mAboveView); 23 addView(rlNotifactions); 24 decor.addView(this);//this指的是上文提到的自定義frameLayout 25 }

 

參考:

https://github.com/adamrocker/simple-side-drawer/blob/master/doc/simple_side_drawer2.pdf 

https://github.com/adamrocker/simple-side-drawer

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM