android 悬浮通知

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;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值