这篇文章将为大家详细讲解有关android6.0版本中怎么实现一个悬浮窗口,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
汉阳ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:028-86922220(备注:SSL证书合作)期待与您的合作!
代码如下:
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):
关于android6.0版本中怎么实现一个悬浮窗口就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。