Android常用控件之悬浮窗(Service实现)
Posted 由此及彼
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android常用控件之悬浮窗(Service实现)相关的知识,希望对你有一定的参考价值。
悬浮窗可以显示在所有应用程序之上,不管在PC机还是android设备上都有这个,最常见的是360的“加速球”
来看下在Android设备上的效果
程序的目录结构如下图
创建Activity后启动Service就关闭
[java] view plain copy- package com.example.floatwnd;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import com.example.floatwnd.service.FloatService;
- public class MainActivity extends Activity
- @Override
- protected void onCreate(Bundle savedInstanceState)
- super.onCreate(savedInstanceState);
- Intent intent = new Intent(MainActivity.this, FloatService.class);
- startService(intent);
- finish();
悬浮窗的主要工作放在Service上处理 [java] view plain copy
- package com.example.floatwnd.service;
- import android.app.Service;
- import android.content.Intent;
- import android.graphics.PixelFormat;
- import android.graphics.Rect;
- import android.os.Handler;
- import android.os.IBinder;
- import android.os.Looper;
- import android.os.Message;
- import android.view.Gravity;
- import android.view.LayoutInflater;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.View.OnTouchListener;
- import android.view.WindowManager;
- import android.view.WindowManager.LayoutParams;
- import android.widget.Button;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.example.floatwnd.R;
- import com.example.floatwnd.utils.SysInfoUtils;
- /**
- * 悬浮窗Service 该服务会在后台一直运行一个悬浮的透明的窗体
- *
- * @author Administrator
- *
- */
- public class FloatService extends Service
- private static final int UPDATE_PIC = 0x100;
- private int statusBarHeight;// 状态栏高度
- private View view;// 透明窗体
- private TextView text = null;
- private Button hideBtn = null;
- private Button updateBtn = null;
- private HandlerUI handler = null;
- private Thread updateThread = null;
- private boolean viewAdded = false;// 透明窗体是否已经显示
- private boolean viewHide = false; // 窗口隐藏
- private WindowManager windowManager;
- private WindowManager.LayoutParams layoutParams;
- @Override
- public IBinder onBind(Intent arg0)
- // TODO Auto-generated method stub
- return null;
- @Override
- public void onCreate()
- // TODO Auto-generated method stub
- super.onCreate();
- createFloatView();
- @Override
- public void onStart(Intent intent, int startId)
- // TODO Auto-generated method stub
- super.onStart(intent, startId);
- System.out.println("------------------onStart");
- viewHide = false;
- refresh();
- @Override
- public void onDestroy()
- // TODO Auto-generated method stubandroid课程表控件悬浮窗Todo应用MVP框架Kotlin完整项目源码
Android 使用WindowManager实现悬浮窗及源码解析
Android 使用WindowManager实现悬浮窗及源码解析