阿里云服务器免费领卷啦。

捡代码论坛-最全的游戏源码下载技术网站!

 找回密码
 立 即 注 册

QQ登录

只需一步,快速开始

搜索
关于源码区的附件失效或欺骗帖, 处理办法
查看: 2055|回复: 0

安卓6.0版本下悬浮窗实现

[复制链接]

4208

主题

210

回帖

12万

积分

管理员

管理员

Rank: 9Rank: 9Rank: 9

积分
126084
QQ
发表于 2017-3-20 23:00:20 | 显示全部楼层 |阅读模式

悬浮窗在安卓中实现起来还是比较容易的,这几天在网上温习了相关资料,运行在我安卓6.0手机上才发现,原来在6.0手机上不是行的。第一反应肯定是权限相关问题,做了相关处理后,果然让悬浮窗原形毕露了。直接贴代码。

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

  4.     @Override
  5.     protected void onCreate(Bundle savedInstanceState) {
  6.         super.onCreate(savedInstanceState);
  7.         setContentView(R.layout.activity_main);
  8.         start_float = (Button) findViewById(R.id.start_float);
  9.         this.start_float.setOnClickListener(new View.OnClickListener() {
  10.             @Override
  11.             public void onClick(View view) {
  12.                 if (Build.VERSION.SDK_INT > 22) {
  13.                     sdk23Permission();
  14.                 } else {
  15.                     startService(new Intent(MainActivity.this, FloatService.class));
  16.                     finish();
  17.                 }
  18.             }
  19.         });
  20.     }

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

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

  48.         }
  49.     }
  50. }
复制代码
对应Service:

  1. public class FloatService extends Service {
  2.     @Nullable
  3.     @Override
  4.     public IBinder onBind(Intent intent) {
  5.         return null;
  6.     }

  7.     @Override
  8.     public void onCreate() {
  9.         FloatViewUtils.getInstance(this).addFloatView();
  10.         super.onCreate();
  11.     }
  12. }
复制代码
简单地FloatView:
  1. public class FloatView extends View {
  2.     public static final int WIDTH = 150;
  3.     public static final int HEIGHT = 150;
  4.     private Paint circlePaint;
  5.     private Paint textPaint;
  6.     private static final String text = "50%";

  7.     public FloatView(Context context) {
  8.         this(context, null, 0);
  9.     }

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

  13.     public FloatView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
  14.         super(context, attrs, defStyleAttr);
  15.         initPaints();
  16.     }

  17.     /**
  18.      * @description 初始化相关画笔Paint
  19.      * @author ldm
  20.      * @time 2017/3/20
  21.      */
  22.     private void initPaints() {
  23.         circlePaint = new Paint();
  24.         circlePaint.setAntiAlias(true);
  25.         circlePaint.setColor(Color.GRAY);
  26.         textPaint = new Paint();
  27.         //设置抗锯齿
  28.         textPaint.setAntiAlias(true);
  29.         //设置字体大小
  30.         textPaint.setTextSize(30);
  31.         //设置颜色
  32.         textPaint.setColor(Color.WHITE);
  33.         //设置(仿)粗体
  34.         textPaint.setFakeBoldText(true);
  35.     }

  36.     @Override
  37.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  38.         setMeasuredDimension(WIDTH, HEIGHT);
  39.     }

  40.     @Override
  41.     protected void onDraw(Canvas canvas) {
  42.         super.onDraw(canvas);
  43.         canvas.drawCircle(WIDTH / 2, HEIGHT / 2, WIDTH / 2, circlePaint);
  44.         float textWidth = textPaint.measureText(text);
  45.         float x = WIDTH / 2 - textWidth / 2;
  46.         Paint.FontMetrics fms = textPaint.getFontMetrics();
  47.         float dy = -(fms.descent + fms.ascent) / 2;
  48.         float y = HEIGHT / 2 + dy;
  49.         canvas.drawText(text, x, y, textPaint);
  50.     }
  51. }
复制代码
以及FloatView管理工具类:

  1. public class FloatViewUtils {
  2.     private static FloatViewUtils instance;
  3.     private Context mContext;
  4.     private WindowManager manager;
  5.     private FloatView floatView;

  6.     private FloatViewUtils(Context mContext) {
  7.         this.mContext = mContext;
  8.         manager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
  9.         floatView = new FloatView(mContext);
  10.     }

  11.     public static FloatViewUtils getInstance(Context mContext) {
  12.         if (null == instance) {
  13.             synchronized (FloatViewUtils.class) {
  14.                 if (null == instance) {
  15.                     instance = new FloatViewUtils(mContext);
  16.                 }
  17.             }
  18.         }
  19.         return instance;
  20.     }

  21.     public void addFloatView() {
  22.         WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
  23.         //悬浮窗口大小
  24.         lp.width = floatView.WIDTH;
  25.         lp.height = floatView.HEIGHT;
  26.         // 调整悬浮窗口位置
  27.         lp.gravity = Gravity.LEFT | Gravity.CENTER;
  28.         // 以屏幕左上角为原点,设置x、y初始值
  29. //        lp.x = 0;
  30. //        lp.y = 0;
  31.         //设置悬浮窗口类型
  32.         lp.type = WindowManager.LayoutParams.TYPE_PHONE;
  33.         //设置悬浮窗口不接受焦点及触摸事件
  34.         lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
  35.         //设置图片格式,效果为背景透明
  36.         lp.format = PixelFormat.RGBA_8888;
  37.         manager.addView(floatView, lp);
  38.     }
  39. }
复制代码
最后不要忘记在AndroidManifest.xml中添加权限(当然还有注册Service):

  1. <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
复制代码

原文地址:
http://blog.csdn.net/true100/article/details/64126121

捡代码论坛-最全的游戏源码下载技术网站! - 论坛版权郑重声明:
1、本主题所有言论和图片纯属会员个人意见,与本论坛立场无关
2、本站所有主题由该帖子作者发表,该帖子作者与捡代码论坛-最全的游戏源码下载技术网站!享有帖子相关版权
3、捡代码论坛版权,详细了解请点击。
4、本站所有内容均由互联网收集整理、网友上传,并且以计算机技术研究交流为目的,仅供大家参考、学习,不存在任何商业目的与商业用途。
5、若您需要商业运营或用于其他商业活动,请您购买正版授权并合法使用。 我们不承担任何技术及版权问题,且不对任何资源负法律责任。
6、如无法链接失效或侵犯版权,请给我们来信:jiandaima@foxmail.com

回复

使用道具 举报

*滑块验证:
您需要登录后才可以回帖 登录 | 立 即 注 册

本版积分规则

技术支持
在线咨询
QQ咨询
3351529868

QQ|手机版|小黑屋|捡代码论坛-专业源码分享下载 ( 陕ICP备15015195号-1|网站地图

GMT+8, 2024-3-29 22:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表