开发是我们有时候需要一些快捷通道,类似ios系统的拖动方块。
Android6.0及其以上版本手机使用悬浮窗功能会正常使用,在23及以上版本下编译,悬浮窗权限默认是关闭没有权限,然在23以下版本下编译悬浮窗权限是开启有权限的。所以在大于23版本下编译时需要去检测悬浮窗权限,并且获取悬浮窗权限,下面我就罗列下怎么去检测悬浮窗权限并且获取悬浮窗权限。
int version = Build.VERSION.SDK_INT;
// 悬浮窗Settings.canDrawOverlays获取权限是否开通
if (version < 23 || (version >= 23 && Settings.canDrawOverlays(this))) {
//初始化小方块的布局
initDragLayout();
return;
}
//弹出是否授权悬浮的dialog
AuthorizationDialogFragment dialogFragment = new AuthorizationDialogFragment();
dialogFragment.show(getSupportFragmentManager(), "AuthorizationDialogFragment");
dialogFragment.setClickListener(new AuthorizationDialogFragment.onClickListener() {
@Override
public void click(int type) {
if (type == 0) {
mainFastView.setVisibility(View.VISIBLE);
home_red_image.setVisibility(AppConstantUtils.isClickNewTask(context) ? View.GONE : View.VISIBLE);
} else {
try {
//同意开启悬浮权限
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
startActivityForResult(intent, 10);
} catch (Exception e) {
}
}
}
});
授权dialog
系统授权界面.jpg
授权之后返回
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 10) {
if (Build.VERSION.SDK_INT >= 23) {
if (Settings.canDrawOverlays(this)) {
//初始化小方块的布局
initDragLayout();
} else {
mainFastView.setVisibility(View.VISIBLE);
home_red_image.setVisibility(AppConstantUtils.isClickNewTask(context) ? View.GONE : View.VISIBLE);
}
}
}
}
@SuppressLint("ResourceAsColor")
public void showAddress() {
if (AppConstantUtils.hasFastButton(this) && null != fast_image) return;
SPUtils.put(this, "hasFastButton", "hasFastButton");
wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
params = ((App) getApplicationContext()).getMywmParams();
fast_image = LayoutInflater.from(getApplicationContext()).inflate(R.layout.fast_market_layout, null);
fast_image.findViewById(R.id.image).setAlpha(0.68f);
params.gravity = Gravity.LEFT | Gravity.TOP;
params.x = (int) SPUtils.get(this, "lastx", 4 * ScreenUtils.getScreenWidth(this) / 5);
params.y = (int) SPUtils.get(this, "lasty", 2 * ScreenUtils.getScreenHeight(this) / 3);
params.height = ScreenUtils.dp2px(this, 48f);
params.width = ScreenUtils.dp2px(this, 48f);
View red_image = fast_image.findViewById(R.id.red_image);
red_image.setVisibility(AppConstantUtils.isClickNewTask(context) ? View.GONE : View.VISIBLE);
// 定义控件 可以触摸 删除一个flag
params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
// | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
params.format = PixelFormat.TRANSLUCENT;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//API26
params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
params.type = WindowManager.LayoutParams.TYPE_PHONE;
}
wm.addView(fast_image, params);
fast_image.setOnTouchListener(new View.OnTouchListener() {
int startX;
int startY;
long startTime;
long endTime;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
startX = (int) event.getRawX();
startY = (int) event.getRawY();
startTime = System.currentTimeMillis();
break;
case MotionEvent.ACTION_MOVE:
int newX = (int) event.getRawX();
int newY = (int) event.getRawY();
int dx = newX - startX;
int dy = newY - startY;
// 更改view对象在窗体上显示的位置.
params.x += dx;
params.y += dy;
if (params.x < 0) {
params.x = 0;
}
if (params.y < 0) {
params.y = 0;
}
if (params.x > wm.getDefaultDisplay().getWidth()) {
params.x = wm.getDefaultDisplay().getWidth();
}
if (params.y > wm.getDefaultDisplay().getHeight()) {
params.y = wm.getDefaultDisplay().getHeight();
}
wm.updateViewLayout(fast_image, params);
// 重新初始化手指的位置
startX = (int) event.getRawX();
startY = (int) event.getRawY();
break;
case MotionEvent.ACTION_UP:
SPUtils.put(BaseAcyivity.this, "lastx", params.x);
SPUtils.put(BaseAcyivity.this, "lasty", params.y);
endTime = System.currentTimeMillis();
if (endTime - startTime < 300) {
EventBus.getDefault().post(new MessageEvent("showFast"));
}
return false;
}
return true;
}
});
}
// 权限添加
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
网友评论