android悬浮窗
- 首先需要申请权限
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (Settings.canDrawOverlays(MainActivity.this)) { Intent intent = new Intent(MainActivity.this,MainService.class); Toast.makeText(MainActivity.this,"已开启Toucher",Toast.LENGTH_SHORT).show(); startService(intent); finish(); }else { //若没有权限,提示获取. Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION); Toast.makeText(MainActivity.this,"需要取得权限以使用悬浮窗",Toast.LENGTH_SHORT).show(); startActivity(intent); finish(); } }else{ Intent intent = new Intent(MainActivity.this, MainService.class); startService(intent); finish(); }
- 开启一个service
package com.lightingcontour.toucher; import android.app.Service; import android.content.Context; import android.content.Intent; import android.graphics.PixelFormat; import android.os.Handler; import android.os.IBinder; import android.os.SystemClock; import android.support.constraint.ConstraintLayout; import android.util.Log; import android.view.Gravity; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.WindowManager; import android.widget.ImageButton; import android.widget.RelativeLayout; import android.widget.Toast; public class MainService extends Service { private static final String TAG = "MainService"; ConstraintLayout toucherLayout; WindowManager.LayoutParams params; WindowManager windowManager; ImageButton imageButton1; //状态栏高度. int statusBarHeight = -1; //不与Activity进行绑定. @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); Log.i(TAG,"MainService Created"); createToucher(); } private void createToucher() { //赋值WindowManager&LayoutParam. params = new WindowManager.LayoutParams(); windowManager = (WindowManager) getApplication().getSystemService(Context.WINDOW_SERVICE); //设置type.系统提示型窗口,一般都在应用程序窗口之上. params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT; //设置效果为背景透明. params.format = PixelFormat.RGBA_8888; //设置flags.不可聚焦及不可使用按钮对悬浮窗进行操控. params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; //设置窗口初始停靠位置. params.gravity = Gravity.LEFT | Gravity.TOP; params.x = 0; params.y = 0; //设置悬浮窗口长宽数据. params.width = 300; params.height = 300; LayoutInflater inflater = LayoutInflater.from(getApplication()); //获取浮动窗口视图所在布局. toucherLayout = (ConstraintLayout) inflater.inflate(R.layout.toucherlayout,null); //添加toucherlayout windowManager.addView(toucherLayout,params); Log.i(TAG,"toucherlayout-->left:" + toucherLayout.getLeft()); Log.i(TAG,"toucherlayout-->right:" + toucherLayout.getRight()); Log.i(TAG,"toucherlayout-->top:" + toucherLayout.getTop()); Log.i(TAG,"toucherlayout-->bottom:" + toucherLayout.getBottom()); //主动计算出当前View的宽高信息. toucherLayout.measure(View.MeasureSpec.UNSPECIFIED,View.MeasureSpec.UNSPECIFIED); //用于检测状态栏高度. int resourceId = getResources().getIdentifier("status_bar_height","dimen","android"); if (resourceId > 0) { statusBarHeight = getResources().getDimensionPixelSize(resourceId); } Log.i(TAG,"状态栏高度为:" + statusBarHeight); //浮动窗口按钮. imageButton1 = (ImageButton) toucherLayout.findViewById(R.id.imageButton1); imageButton1.setOnClickListener(new View.OnClickListener() { long[] hints = new long[2]; @Override public void onClick(View v) { Log.i(TAG,"点击了"); System.arraycopy(hints,1,hints,0,hints.length -1); hints[hints.length -1] = SystemClock.uptimeMillis(); if (SystemClock.uptimeMillis() - hints[0] >= 700) { Log.i(TAG,"要执行"); Toast.makeText(MainService.this,"连续点击两次以退出",Toast.LENGTH_SHORT).show(); }else { Log.i(TAG,"即将关闭"); stopSelf(); } } }); imageButton1.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { Log.i(TAG,"event:->"+event.getAction()); params.x = (int) event.getRawX() - 150; params.y = (int) event.getRawY() - 150 - statusBarHeight; windowManager.updateViewLayout(toucherLayout,params); return false; } }); } @Override public void onDestroy() { if (imageButton1 != null) { windowManager.removeView(toucherLayout); } super.onDestroy(); } }
- 添加权限
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/> <uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW"/>
Dana.Lee
To:Dana_Lee1016@126.com