欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

详解android6.0版本下悬浮窗实现

时间:2020-11-23 12:18:31|栏目:Android代码|点击:

悬浮窗在安卓中实现起来还是比较容易的,这几天在网上温习了相关资料,运行在我安卓6.0手机上才发现,原来在6.0手机上不是行的。

第一反应肯定是权限相关问题,做了相关处理后,果然让悬浮窗原形毕露了。直接贴代码。

public class MainActivity extends AppCompatActivity {
  private static final int ALERT_WINDOW_PERMISSION_CODE = 100;
  private Button start_float;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    start_float = (Button) findViewById(R.id.start_float);
    this.start_float.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        if (Build.VERSION.SDK_INT > 22) {
          sdk23Permission();
        } else {
          startService(new Intent(MainActivity.this, FloatService.class));
          finish();
        }
      }
    });
  }

  /**
   * @description 安卓6.0下权限处理
   * @author ldm
   * @time 2017/3/20 15:00
   */
  public void sdk23Permission() {
    if (!Settings.canDrawOverlays(this)) {
      Toast.makeText(MainActivity.this, "当前无权限使用悬浮窗,请授权!", Toast.LENGTH_SHORT).show();
      Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
          Uri.parse("package:" + getPackageName()));
      startActivityForResult(intent, ALERT_WINDOW_PERMISSION_CODE);
    } else {
      startService(new Intent(MainActivity.this, FloatService.class));
      finish();
    }
  }

  /**
   * 用户返回
   */
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == ALERT_WINDOW_PERMISSION_CODE) {
      if (!Settings.canDrawOverlays(this)) {
        Toast.makeText(MainActivity.this, "权限授予失败,无法开启悬浮窗", Toast.LENGTH_SHORT).show();
      } else {
        startService(new Intent(MainActivity.this, FloatService.class));
        finish();
      }

    }
  }
}

对应Service:

public class FloatService extends Service {
  @Nullable
  @Override
  public IBinder onBind(Intent intent) {
    return null;
  }

  @Override
  public void onCreate() {
    FloatViewUtils.getInstance(this).addFloatView();
    super.onCreate();
  }
}

简单地FloatView:

public class FloatView extends View {
  public static final int WIDTH = 150;
  public static final int HEIGHT = 150;
  private Paint circlePaint;
  private Paint textPaint;
  private static final String text = "50%";

  public FloatView(Context context) {
    this(context, null, 0);
  }

  public FloatView(Context context, @Nullable AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public FloatView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initPaints();
  }

  /**
   * @description 初始化相关画笔Paint
   * @author ldm
   * @time 2017/3/20
   */
  private void initPaints() {
    circlePaint = new Paint();
    circlePaint.setAntiAlias(true);
    circlePaint.setColor(Color.GRAY);
    textPaint = new Paint();
    //设置抗锯齿
    textPaint.setAntiAlias(true);
    //设置字体大小
    textPaint.setTextSize(30);
    //设置颜色
    textPaint.setColor(Color.WHITE);
    //设置(仿)粗体
    textPaint.setFakeBoldText(true);
  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    setMeasuredDimension(WIDTH, HEIGHT);
  }

  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawCircle(WIDTH / 2, HEIGHT / 2, WIDTH / 2, circlePaint);
    float textWidth = textPaint.measureText(text);
    float x = WIDTH / 2 - textWidth / 2;
    Paint.FontMetrics fms = textPaint.getFontMetrics();
    float dy = -(fms.descent + fms.ascent) / 2;
    float y = HEIGHT / 2 + dy;
    canvas.drawText(text, x, y, textPaint);
  }
}

以及FloatView管理工具类:

public class FloatViewUtils {
  private static FloatViewUtils instance;
  private Context mContext;
  private WindowManager manager;
  private FloatView floatView;

  private FloatViewUtils(Context mContext) {
    this.mContext = mContext;
    manager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
    floatView = new FloatView(mContext);
  }

  public static FloatViewUtils getInstance(Context mContext) {
    if (null == instance) {
      synchronized (FloatViewUtils.class) {
        if (null == instance) {
          instance = new FloatViewUtils(mContext);
        }
      }
    }
    return instance;
  }

  public void addFloatView() {
    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    //悬浮窗口大小
    lp.width = floatView.WIDTH;
    lp.height = floatView.HEIGHT;
    // 调整悬浮窗口位置
    lp.gravity = Gravity.LEFT | Gravity.CENTER;
    // 以屏幕左上角为原点,设置x、y初始值
//    lp.x = 0;
//    lp.y = 0;
    //设置悬浮窗口类型
    lp.type = WindowManager.LayoutParams.TYPE_PHONE;
    //设置悬浮窗口不接受焦点及触摸事件
    lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
    //设置图片格式,效果为背景透明
    lp.format = PixelFormat.RGBA_8888;
    manager.addView(floatView, lp);
  }
}

最后不要忘记在AndroidManifest.xml中添加权限(当然还有注册Service):

复制代码 代码如下:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

上一篇:分享Android平板电脑上开发应用程序不能全屏显示的问题解决

栏    目:Android代码

下一篇:Android使用ViewDragHelper实现QQ6.X最新版本侧滑界面效果实例代码

本文标题:详解android6.0版本下悬浮窗实现

本文地址:http://www.codeinn.net/misctech/25452.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有