zoukankan      html  css  js  c++  java
  • 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     }
  • 相关阅读:
    POJ
    luogu- P1373 小a和uim之大逃离 DP 四维,其中一维记录差值
    牛客国庆集训派对Day3 B Tree(树形dp + 组合计数)
    【CF 1059C】 Sequence Transformation 数学
    POJ
    牛客国庆集训派对Day6 A Birthday 费用流
    Treap + 无旋转Treap 学习笔记
    牛客2018国庆集训派对Day3 I Metropolis 多源最短路径
    Gym
    CodeForces
  • 原文地址:https://www.cnblogs.com/huhe/p/14131553.html
Copyright © 2011-2022 走看看