对于一些提示我们并不依赖与任何界面,如上图。
有很多方式可以实现,方式是自定义Toast。但是这种方式在华为手机(还有一些其他手机)上不能点击Toast时间,需要我们自己setOnTouchListener来处理。并且这样使得整个App都会相应Touch事件。需要自己处理。
具体处理思路:自定义Toast,设置setOnTouchListener,然后判断是否在点击的区域,再做触摸防抖处理。虽然麻烦点,也算是完美解决。贴上代码
Toast mCustomToast = new Toast(HiFrameworkApplication.getInstance());
mCustomToast.setDuration(Toast.LENGTH_SHORT);
mCustomToast.setGravity(Gravity.BOTTOM, 0, 0);
final LayoutInflater inflater = LayoutInflater.from(HiFrameworkApplication.getInstance());
final View layoutView = inflater.inflate(R.layout.fp_fpbphone_custom_alarm_toast, null);
RelativeLayout root = layoutView.findViewById(R.id.fp_fpbphone_root);
TextView mTvTime = layoutView.findViewById(R.id.tv_fp_fpbphone_custom_alarm_toast_time);
TextView mTvContent = layoutView.findViewById(R.id.tv_fp_fpbphone_custom_alarm_toast_content);
TextView click = layoutView.findViewById(R.id.tv_fp_fpbphone_custom_alarm_toast_detail);
mTvTime.setText(time);
mTvContent.setText(content);
//有id才可点击
click.setFocusable(true);
click.setVisibility(TextUtils.isEmpty(alarmId) ? View.GONE : View.VISIBLE);
layoutView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//判断当前点击位置
final int rawY = (int) event.getRawY();
final int rawX = (int) event.getRawX();
//点击的位置必须大于屏幕高度-控件的高度
Log.d("azy", "width" + layoutView.getWidth() + " height" + layoutView.getHeight());
// 判断是不是在区域范围内
if (isInView(layoutView,rawX,rawY)){
LogUtil.d("azy", "mScreenHight " + mScreenHight + " rawY " + rawY);
if (DuplicateClicksUtil.isFastClick()) {
LogUtil.d("azy", "onTouch");
EventBus.getDefault().post(new AlarmClickEvent(alarmId));
Intent intent =
new Intent(HiFrameworkApplication.getInstance(), AlarmDetailActivity.class);
Bundle bundle = new Bundle();
bundle.putString(Cons.INTENT_ALARM_ID, alarmId);
intent.putExtras(bundle);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
HiFrameworkApplication.getInstance().startActivity(intent);
}
}
return true;
}
});
try {
Object mTN;
mTN = getField(mCustomToast, "mTN");
if (mTN != null) {
Object mParams = getField(mTN, "mParams");
if (mParams != null
&& mParams instanceof WindowManager.LayoutParams) {
final WindowManager.LayoutParams params = (WindowManager.LayoutParams) mParams;
//显示与隐藏动画
// params.windowAnimations = R.style.ClickToast;
//Toast可点击,这里的属性设置很重要
params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
//设置viewgroup宽高
params.width = WindowManager.LayoutParams.MATCH_PARENT; //设置Toast宽度为屏幕宽度
params.height = WindowManager.LayoutParams.WRAP_CONTENT; //设置高度
}
}
} catch (Exception e) {
e.printStackTrace();
}
mCustomToast.setView(layoutView);
mCustomToast.show();
关于判断对应控件区域的代码如下:
private Rect mViewRect;
private boolean isInView(View view,int x, int y){
if (mViewRect == null){
mViewRect = new Rect();
}
view.getDrawingRect(mViewRect);
int[] location = new int[2];
view.getLocationOnScreen(location);
mViewRect.left = location[0];
mViewRect.top = location[1];
mViewRect.right = mViewRect.right + location[0];
mViewRect.bottom = mViewRect.bottom + location[1];
return mViewRect.contains(x,y);
}
实现方式:直接使用WindowManager来添加对应的布局,这个也是不跟Acitivty绑定的全局弹窗,甚至在app退到后台还可以继续显示。当然按照业务需求在必要的时候removeView。
核心代码如下:
systemService = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
final LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutView = inflater.inflate(R.layout.fp_fpbphone_custom_alarm_toast, null);
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
params.type = WindowManager.LayoutParams.TYPE_PHONE;
}
//这个地方一定要这样设置,不然要么是布局之外不能接受事件,要么是布局里面和返回按键接收不到事件。
params.flags = WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
params.gravity = Gravity.TOP | Gravity.LEFT;
// 设置窗口宽度和高度
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.x = 50;
systemService.addView(layoutView,params);
最后,有几个需要注意的点:
1.一定要设置权限。
<!-- 弹窗权限-->
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
2.权限在6.0系统以上要手动设置
@RequiresApi(api = Build.VERSION_CODES.M)
private void requestPermission() {
if (!Settings.canDrawOverlays(this)) {
//未授权限,请求权限
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivityForResult(intent,0);
}
}