今天给大家写一个这个Android悬浮窗的功能,这个功能一般在360,酷狗(桌面歌词),网易云音乐(桌面歌词)上面用到,一般开发是很少碰到这个功能的,但是这个悬浮窗功能可以帮助理解Android的绘制机制。
这个功能大概是这样的,我们进一个Activity,在onCreate里面开启一个Service,在服务器里添加一个悬浮窗,然后退出Activity.
Activity:
package com.example.library;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.Toast;
import com.example.library.service.MainService;
public class SecondActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//不需要setContentView
if (Build.VERSION.SDK_INT >= 23) {
if (Settings.canDrawOverlays(SecondActivity.this)) {
Intent intent = new Intent(SecondActivity.this, MainService.class);
Toast.makeText(SecondActivity.this,"已开启Toucher",Toast.LENGTH_SHORT).show();
startService(intent);
finish();
} else {
//若没有权限,提示获取.
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
Toast.makeText(SecondActivity.this,"需要取得权限以使用悬浮窗",Toast.LENGTH_SHORT).show();
startActivity(intent);
}
} else {
//SDK在23以下,不用管.
Intent intent = new Intent(SecondActivity.this, MainService.class);
startService(intent);
finish();
}
}
}
然后再是Service:
package com.example.library.service;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.example.library.R;
public class MainService extends Service {
//Log用的TAG
private static final String TAG = "MainService";
//布局参数.
WindowManager.LayoutParams params;
//实例化的WindowManager.
WindowManager windowManager;
RelativeLayout toucherLayout;
ImageButton imageButton1;
//状态栏高度.(接下来会用到)
int statusBarHeight = -1;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.i("MainService" , "onCreate==onCreate");
//OnCreate中来生成悬浮窗.
createToucher();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("MainService" , "onCreate==onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
private void createToucher()
{
//赋值WindowManager&LayoutParam.
params = new WindowManager.LayoutParams();
windowManager = (WindowManager)getBaseContext(). getSystemService(Context.WINDOW_SERVICE);
//设置type.系统提示型窗口,一般都在应用程序窗口之上.
//这个是8.0系统以下的
// params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
//这个是8.0系统以上
params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
//设置效果为背景透明.
params.format = PixelFormat.RGBA_8888;
//设置flags.不可聚焦及不可使用按钮对悬浮窗进行操控.
params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
//设置窗口初始停靠位置.
params.gravity = Gravity.LEFT | Gravity.TOP;
params.x = 0;
params.y = 0;
//设置悬浮窗口长宽数据.
//注意,这里的width和height均使用px而非dp.这里我偷了个懒
//如果你想完全对应布局设置,需要先获取到机器的dpi
//px与dp的换算为px = dp * (dpi / 160).
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.height = 300;
LayoutInflater inflater = LayoutInflater.from(getApplication());
toucherLayout = (RelativeLayout) inflater.inflate(R.layout.layout_main , null);
imageButton1 = toucherLayout.findViewById(R.id.imageView);
//主动计算出当前View的宽高信息.
// toucherLayout.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
//用于检测状态栏高度.
int resourceId = getResources().getIdentifier("status_bar_height","dimen","android");
if (resourceId > 0)
{
statusBarHeight = getResources().getDimensionPixelSize(resourceId);
}
Log.i(TAG,"状态栏高度为:" + statusBarHeight);
//其他代码...
imageButton1.setOnClickListener(new View.OnClickListener() {
long[] hints = new long[2];
@Override
public void onClick(View v) {
Log.i(TAG,"点击了");
System.arraycopy(hints,1,hints,0,hints.length -1);
hints[hints.length -1] = SystemClock.uptimeMillis();
if (SystemClock.uptimeMillis() - hints[0] >= 700)
{
Log.i(TAG,"要执行");
Toast.makeText(MainService.this,"连续点击两次以退出", Toast.LENGTH_SHORT).show();
}else
{
Log.i(TAG,"即将关闭");
stopSelf();
}
}
});
imageButton1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//ImageButton我放在了布局中心,布局一共300dp
params.x = (int) event.getRawX() - 150;
//这就是状态栏偏移量用的地方
params.y = (int) event.getRawY() - 150 - statusBarHeight;
windowManager.updateViewLayout(toucherLayout,params);
return false;
}
});
windowManager.addView(toucherLayout , params);
}
@Override
public void onDestroy()
{
if (windowManager != null){
windowManager.removeView(toucherLayout);
}
super.onDestroy();
}
}
然后配置Service
然后就是权限配置
然后就是去手机系统设置打开:在其他应用上层显示
然后效果出来啦
以上就是一个简单的悬浮窗口功能 说到这里就不难明白,Activity并不是我们唯一显示布局的组件,这个是为什么,原来我们的Activity只是一个用来承载我们布局的中间件,它本身不负责绘制,我们写的xml文件是在Window对象里面执行的,Window的实现类是PhoneWindow,PhoneWindow里面有一个WindowManager,然后把我们的View传给WinowManagerImpl,再传给WindowManagerGlobal,再传给ViewRootImpl,ViewRootImpl里面负责事件处理跟图像绘制,所以Activity其实也是把view给WindowManager处理了,只是Activity会负责整个视图的生命周期。这个悬浮窗就是脱离了Activity的限制。
在这里我们设置了系统窗口类型,只是系统管制了,需要手动设置权限而已。
一个Activity里有一个Window对象,这个Window对象就包含了布局,这个Window对象必须依附Activity,我们添加Dialog的时候其实就是加一个Window对象,所以我们在新建Dialog的时候不能传Context,而必须传Activity实例对象的原因。
像我们的事件分发机制,为什么是Activity–>ViewGroup–>View的流程,也就有了答案,因为点击事件是由底层硬件发出的,而Activity是我们承载布局的组件(中间件),从底层往上传递,肯定是Activity在前面。