android-windowManager使用实例
作者:互联网
public static boolean isStarted;
private WindowManager windowManager;
private WindowManager.LayoutParams layoutParams;
private View displayView;
private Handler mHandler = new Handler();
private Button accOn;
private Button accOff;
@Override
public void onCreate() {
super.onCreate();
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
layoutParams = new WindowManager.LayoutParams();
layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
layoutParams.format = PixelFormat.RGBA_8888;
layoutParams.gravity = Gravity.CENTER | Gravity.TOP;
// layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
layoutParams.width = 250;
layoutParams.height = 50;
layoutParams.x = 500;
layoutParams.y = 0;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {//兼容android8.0以上需要启动前台服务
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel mChannel = new NotificationChannel("CHANNEL_ID_STRING", "检查连接状态", NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(mChannel);
Notification notification = new Notification.Builder(getApplicationContext(), "CHANNEL_ID_STRING").build();
startForeground(1, notification);
}
isStarted = true;
showFloatingWindow();
return super.onStartCommand(intent, flags, startId);
}
private String address;
private void showFloatingWindow() {
if (Settings.canDrawOverlays(this)) {
LayoutInflater layoutInflater = LayoutInflater.from(this);
displayView = layoutInflater.inflate(R.layout.window, null);
accOn = displayView.findViewById(R.id.acc_on);
accOn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
accOff = displayView.findViewById(R.id.acc_off);
accOff.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
windowManager.addView(displayView, layoutParams);
}
}
标签:layoutParams,windowManager,WindowManager,private,LayoutParams,实例,new,android,pub 来源: https://www.cnblogs.com/huhe/p/14131553.html