实现上线通知悬浮窗,5秒后自动关闭。

 

实现思路:

1、通过开启一个service,在servcie中通过windowManager添加view实现在任意activity都能展示全局悬浮窗。

2、通过倒计时countDownTimer实现5秒关闭功能。

3、通过broadcast通知启动是否。

package com.vrv.emm.service;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.CountDownTimer;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;

import com.vrv.emm.IMApp;
import com.vrv.emm.R;
import com.vrv.emm.ui.circle.BroadcastConstant;

/**
 * Created by wxk on 2018/5/11.
 */

public class PCLoginRemindService extends Service {
    private ShowCountDownTimer myCountDownTimer;
    private PCloginReciver pCloginReciver;
    private WindowManager window;
    private View view;
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        pCloginReciver = new PCloginReciver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BroadcastConstant.ACTION_PC_LOGIN_SUCCESS);
        registerReceiver(pCloginReciver,intentFilter);
        myCountDownTimer = new ShowCountDownTimer(5000,5000);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);

    }
    public void MyDialogShow(){
        view = View.inflate(IMApp.getAppContext(), R.layout.layout_pclogin,null);
        window = (WindowManager) getSystemService(IMApp.getAppContext().WINDOW_SERVICE);
        WindowManager.LayoutParams wmParams = new WindowManager.LayoutParams();
        wmParams.windowAnimations = R.style.pc_login_anim_view;
        wmParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        wmParams.gravity = Gravity.LEFT | Gravity.TOP;
        wmParams.x=0;
        wmParams.y=0;
        wmParams.format = PixelFormat.RGBA_8888;//设置背景色为透明否则布局中透明度无效
        wmParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.addView(view, wmParams);
        View linearLayout = view.findViewById(R.id.pc_login_success);
        myCountDownTimer.start();
        linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View mview) {
                if (myCountDownTimer!=null){
                    myCountDownTimer.cancel();
                }
                try {
                    if (window!=null&&view!=null){
                        window.removeView(view);
                    }
                }catch (Exception e){
                }
            }
        });
    }

    @Override
    public void onDestroy() {
        unregisterReceiver(pCloginReciver);
        super.onDestroy();
    }

    public class PCloginReciver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            try {
                if (window != null && view != null) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                        if (view.isAttachedToWindow()) {
                            window.removeView(view);
                        }
                    } else {
                        window.removeView(view);
                    }
                }
            }catch (Exception e){
            }
            MyDialogShow();
        }
    }
    public class ShowCountDownTimer extends CountDownTimer {

        public ShowCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long l) {

        }

        @Override
        public void onFinish() {
            try {
                if (window!=null&&view!=null){
                    window.removeView(view);
                }
            }catch (Exception e){
            }
        }
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值