對(duì)于一些提示我們并不依賴與任何界面酒觅,如上圖撮执。
有很多方式可以實(shí)現(xiàn),方式是自定義Toast舷丹。但是這種方式在華為手機(jī)(還有一些其他手機(jī))上不能點(diǎn)擊Toast時(shí)間,需要我們自己setOnTouchListener來處理蜓肆。并且這樣使得整個(gè)App都會(huì)相應(yīng)Touch事件颜凯。需要自己處理。
具體處理思路:自定義Toast仗扬,設(shè)置setOnTouchListener症概,然后判斷是否在點(diǎn)擊的區(qū)域,再做觸摸防抖處理早芭。雖然麻煩點(diǎn)彼城,也算是完美解決。貼上代碼
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才可點(diǎn)擊
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) {
//判斷當(dāng)前點(diǎn)擊位置
final int rawY = (int) event.getRawY();
final int rawX = (int) event.getRawX();
//點(diǎn)擊的位置必須大于屏幕高度-控件的高度
Log.d("azy", "width" + layoutView.getWidth() + " height" + layoutView.getHeight());
// 判斷是不是在區(qū)域范圍內(nèi)
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;
//顯示與隱藏動(dòng)畫
// params.windowAnimations = R.style.ClickToast;
//Toast可點(diǎn)擊退个,這里的屬性設(shè)置很重要
params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
//設(shè)置viewgroup寬高
params.width = WindowManager.LayoutParams.MATCH_PARENT; //設(shè)置Toast寬度為屏幕寬度
params.height = WindowManager.LayoutParams.WRAP_CONTENT; //設(shè)置高度
}
}
} catch (Exception e) {
e.printStackTrace();
}
mCustomToast.setView(layoutView);
mCustomToast.show();
關(guān)于判斷對(duì)應(yīng)控件區(qū)域的代碼如下:
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);
}
實(shí)現(xiàn)方式:直接使用WindowManager來添加對(duì)應(yīng)的布局募壕,這個(gè)也是不跟Acitivty綁定的全局彈窗,甚至在app退到后臺(tái)還可以繼續(xù)顯示语盈。當(dāng)然按照業(yè)務(wù)需求在必要的時(shí)候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;
}
//這個(gè)地方一定要這樣設(shè)置,不然要么是布局之外不能接受事件刀荒,要么是布局里面和返回按鍵接收不到事件代嗤。
params.flags = WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
params.gravity = Gravity.TOP | Gravity.LEFT;
// 設(shè)置窗口寬度和高度
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.x = 50;
systemService.addView(layoutView,params);
最后,有幾個(gè)需要注意的點(diǎn):
1.一定要設(shè)置權(quán)限缠借。
<!-- 彈窗權(quán)限-->
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
2.權(quán)限在6.0系統(tǒng)以上要手動(dòng)設(shè)置
@RequiresApi(api = Build.VERSION_CODES.M)
private void requestPermission() {
if (!Settings.canDrawOverlays(this)) {
//未授權(quán)限干毅,請(qǐng)求權(quán)限
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivityForResult(intent,0);
}
}