实现思路:
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){
}
}
}
}