android-windowManager使用实例

 1 public static boolean isStarted;
 2     private WindowManager windowManager;
 3     private WindowManager.LayoutParams layoutParams;
 4     private View displayView;
 5 
 6     private Handler mHandler = new Handler();
 7     private Button accOn;
 8     private Button accOff;
 9 
10     @Override
11     public void onCreate() {
12         super.onCreate();
13         windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
14         layoutParams = new WindowManager.LayoutParams();
15         layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
16         layoutParams.format = PixelFormat.RGBA_8888;
17         layoutParams.gravity = Gravity.CENTER | Gravity.TOP;
18 //        layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
19         layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
20         layoutParams.width = 250;
21         layoutParams.height = 50;
22         layoutParams.x = 500;
23         layoutParams.y = 0;
24     }
25 
26     @Override
27     public IBinder onBind(Intent intent) {
28         return null;
29     }
30 
31     @Override
32     public int onStartCommand(Intent intent, int flags, int startId) {
33         if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {//兼容android8.0以上需要启动前台服务
34             NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
35             NotificationChannel mChannel = new NotificationChannel("CHANNEL_ID_STRING", "检查连接状态", NotificationManager.IMPORTANCE_HIGH);
36             notificationManager.createNotificationChannel(mChannel);
37             Notification notification = new Notification.Builder(getApplicationContext(), "CHANNEL_ID_STRING").build();
38             startForeground(1, notification);
39         }
40         isStarted = true;
41         showFloatingWindow();
42         return super.onStartCommand(intent, flags, startId);
43     }
44 
45     private String address;
46     private void showFloatingWindow() {
47         if (Settings.canDrawOverlays(this)) {
48             LayoutInflater layoutInflater = LayoutInflater.from(this);
49             displayView = layoutInflater.inflate(R.layout.window, null);
50             accOn = displayView.findViewById(R.id.acc_on);
51             accOn.setOnClickListener(new View.OnClickListener() {
52                 @Override
53                 public void onClick(View view) {
54 
55                 }
56             });
57             accOff = displayView.findViewById(R.id.acc_off);
58             accOff.setOnClickListener(new View.OnClickListener() {
59                 @Override
60                 public void onClick(View view) {
61                     
62                 }
63             });
64             windowManager.addView(displayView, layoutParams);
65         }
66     }

 

posted on 2020-12-14 09:26  Ants_hu  阅读(406)  评论(0编辑  收藏  举报

导航