public class FloatView {
TextView txtTitle;
TextView txtContent;
TextView txtTime;
private String keyWord;
private int type;
private static Bundle bundle;
private WindowManager wm;
private WindowManager.LayoutParams wmParams;
private View view;
private Context context;
CountDownTimer countDownTimer;
private long timeIndex = 6000;
private long timeTemp = 500;
private static FloatView floatView;
private JPushEntity jPushEntity;
public static FloatView getInstance(Bundle bundle, Context context) {
if (floatView == null) {
floatView = new FloatView(bundle, context);
}
floatView.bundle = bundle;
return floatView;
}
private FloatView() {
}
private FloatView(Bundle bundle, Context context) {
this.context = context;
this.bundle = bundle;
}
private void createWindowManager(View view) {
getWindowManager(view);
}
private WindowManager getWindowManager(View view) {
if (wm == null) {
wm = (WindowManager) context.getSystemService(context.WINDOW_SERVICE);
wm.addView(view, createLayoutParams());
} else {
wm.updateViewLayout(view, createLayoutParams());
}
return wm;
}
private WindowManager.LayoutParams createLayoutParams() {
if (wmParams == null) {
wmParams = new WindowManager.LayoutParams();
wmParams.type = WindowManager.LayoutParams.TYPE_TOAST;
wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
/**
*这里的flags也很关键
*代码实际是wmParams.flags |= FLAG_NOT_FOCUSABLE;
*40的由来是wmParams的默认属性(32)+ FLAG_NOT_FOCUSABLE(8)
*/
// wmParams.alpha= 0.89f;
wmParams.format = PixelFormat.TRANSPARENT;
wmParams.gravity = Gravity.TOP;
wmParams.width = WindowManager.LayoutParams.MATCH_PARENT;
wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
}
return wmParams;
}
public void initViews() {
createView();
setData(bundle);
createWindowManager(view);
getCountDownTimer(timeIndex, timeTemp);
countDownTimer.start();
}
private void setData(Bundle bundle) {
String title = "", message = "";
jPushEntity = new JPushEntity(bundle);
type = jPushEntity.getType();
keyWord = jPushEntity.getKeyWord();
title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
message = bundle.getString(JPushInterface.EXTRA_ALERT);
createView();
txtTitle.setText(title);
txtContent.setText(message);
txtTime.setText(DateUtil.getCurrentTimeHHMM());
}
private View createView() {
if (view == null) {
LayoutInflater inflater = LayoutInflater.from(context);
view = inflater.inflate(R.layout.float_view_layout, null);
if(view!=null&&view.getBackground()!=null){
view.getBackground().setAlpha(204);//0~255透明度值
}
txtTitle = (TextView) view.findViewById(R.id.txt_title);
txtContent = (TextView) view.findViewById(R.id.txt_content);
txtTime = (TextView) view.findViewById(R.id.txt_time);
}
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
ActivityUtils.JpushJump(context, type, keyWord);
JPushInterface.clearNotificationById(context, bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID));
}
});
return view;
}
private void dismiss() {
if (wm != null && view != null) {
wm.removeView(view);
}
if (countDownTimer != null)
countDownTimer.cancel();
wm = null;
wmParams = null;
view = null;
}
// private CountDownTimer getCountTimerInstance(long timeIndex, long timeTemp) {
// if (countDownTimer != null) {
// countDownTimer.cancel();
// }
// countDownTimer = getCountDownTimer(timeIndex, timeTemp);
// return countDownTimer;
// }
private CountDownTimer getCountDownTimer(final long timeIndex, long timeTemp) {
if (countDownTimer != null) {
countDownTimer.cancel();
}
countDownTimer = new CountDownTimer(timeIndex, timeTemp) {
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
dismiss();
}
};
return countDownTimer;
}
}
android 悬浮通知
最新推荐文章于 2023-06-28 01:45:00 发布