|
|
@@ -1,4 +1,97 @@
|
|
|
package com.izouma.mobilecybergames;
|
|
|
|
|
|
-public class FloatWindowService {
|
|
|
+import android.app.Service;
|
|
|
+import android.content.Intent;
|
|
|
+import android.graphics.Color;
|
|
|
+import android.graphics.PixelFormat;
|
|
|
+import android.os.Build;
|
|
|
+import android.os.IBinder;
|
|
|
+import android.provider.Settings;
|
|
|
+import android.support.annotation.Nullable;
|
|
|
+import android.view.Gravity;
|
|
|
+import android.view.MotionEvent;
|
|
|
+import android.view.View;
|
|
|
+import android.view.WindowManager;
|
|
|
+import android.widget.Button;
|
|
|
+
|
|
|
+public class FloatWindowService extends Service {
|
|
|
+ private WindowManager windowManager;
|
|
|
+ private WindowManager.LayoutParams layoutParams;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onCreate() {
|
|
|
+ super.onCreate();
|
|
|
+ windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
|
|
|
+ layoutParams = new WindowManager.LayoutParams();
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
+ layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
|
|
|
+ } else {
|
|
|
+ layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
|
|
|
+ }
|
|
|
+ layoutParams.format = PixelFormat.RGBA_8888;
|
|
|
+ layoutParams.gravity = Gravity.LEFT | Gravity.TOP;
|
|
|
+ layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
|
|
|
+ layoutParams.width = 500;
|
|
|
+ layoutParams.height = 100;
|
|
|
+ layoutParams.x = 300;
|
|
|
+ layoutParams.y = 300;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Nullable
|
|
|
+ @Override
|
|
|
+ public IBinder onBind(Intent intent) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int onStartCommand(Intent intent, int flags, int startId) {
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//判断系统版本
|
|
|
+ if (Settings.canDrawOverlays(this)) {
|
|
|
+ Button button = new Button(getApplicationContext());
|
|
|
+ button.setText("我是个button窗口");
|
|
|
+ button.setBackgroundColor(Color.BLUE);
|
|
|
+ windowManager.addView(button, layoutParams);
|
|
|
+
|
|
|
+ button.setOnTouchListener(new FloatingOnTouchListener());
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ Button button = new Button(getApplicationContext());
|
|
|
+ button.setText("我是个button窗口");
|
|
|
+ button.setBackgroundColor(Color.BLUE);
|
|
|
+ windowManager.addView(button, layoutParams);
|
|
|
+
|
|
|
+ button.setOnTouchListener(new FloatingOnTouchListener());
|
|
|
+ }
|
|
|
+
|
|
|
+ return super.onStartCommand(intent, flags, startId);
|
|
|
+ }
|
|
|
+
|
|
|
+ private class FloatingOnTouchListener implements View.OnTouchListener {
|
|
|
+ private int x;
|
|
|
+ private int y;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean onTouch(View view, MotionEvent event) {
|
|
|
+ switch (event.getAction()) {
|
|
|
+ case MotionEvent.ACTION_DOWN:
|
|
|
+ x = (int) event.getRawX();
|
|
|
+ y = (int) event.getRawY();
|
|
|
+ break;
|
|
|
+ case MotionEvent.ACTION_MOVE:
|
|
|
+ int nowX = (int) event.getRawX();
|
|
|
+ int nowY = (int) event.getRawY();
|
|
|
+ int movedX = nowX - x;
|
|
|
+ int movedY = nowY - y;
|
|
|
+ x = nowX;
|
|
|
+ y = nowY;
|
|
|
+ layoutParams.x = layoutParams.x + movedX;
|
|
|
+ layoutParams.y = layoutParams.y + movedY;
|
|
|
+ windowManager.updateViewLayout(view, layoutParams);
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|