android6.0版本中怎么实现一个悬浮窗口
短信预约 -IT技能 免费直播动态提醒
这篇文章将为大家详细讲解有关android6.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(); } } }); } 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(); } 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"/>
关于android6.0版本中怎么实现一个悬浮窗口就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341